How to center the camera so that marker is at the bottom of screen? (Google map api V2 Android)
I might edit this answer later to provide some code, but what I think could work is this:
- Get
LatLng
(LatLng M) of the clicked marker. - Convert LatLng M to a
Point
(Point M) using theProjection.toScreenLocation(LatLng)
method. This gives you the location of the marker on the device's display (in pixels). - Compute the location of a point (New Point) that's above Point M by half of the map's height.
- Convert the New Point back to
LatLng
and center the map on it.
Look here for my answer on how to get the map's height.
// googleMap is a GoogleMap object
// view is a View object containing the inflated map
// marker is a Marker object
Projection projection = googleMap.getProjection();
LatLng markerPosition = marker.getPosition();
Point markerPoint = projection.toScreenLocation(markerPosition);
Point targetPoint = new Point(markerPoint.x, markerPoint.y - view.getHeight() / 2);
LatLng targetPosition = projection.fromScreenLocation(targetPoint);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(targetPosition), 1000, null);
centering the camera in Google Maps V2
After reading some additional docs, I found that the following code worked...
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(Lat, Lon))
.zoom(15)
.bearing(0)
.tilt(45)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.addMarker(new MarkerOptions()
.position(new LatLng(Lat, Lon))
.title("Phone Location")
);
I think my original code should have also worked.
Gary
Center android google map on group of markers?
You can animate your camera within specific bounds. Let's say you have an ArrayList of markers, something like (this could also be a list of LatLngs, doubles, anything that contains the latitude/longitude for your marker)
List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
You can then make sure they are all visible on your map by doing the following
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLng position;
for(int i = 0; i < markers.size(); i++){
position = markers.get(i).getPosition();
builder.include(new LatLng(position.latitude, position.longitude));
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
Google Map API V2 - How do I keep a marker in the center of the screen while user is scrolling the map?
I do not think that you can actually keep a marker in the middle of the screen and float it easily. My suggestion is to trick your user. Add your marker image onto the map like a button. you can place it in the middle of the screen using the xml layout. Then when your user selects a location just retrieve the gps coordinates from the middle of the screen.
On a side note you could also just make your marker draggable Then the user could drag it around the map.
mMap.addMarker(new MarkerOptions().position(coordinate)
.title("Your Title")
.snippet("Please move the marker if needed.")
.draggable(true));
How to keep the map marker in the center of the screen at all times? Android
Don't use a real marker. Put the MapFragment
or MapView
in a layout with a fake marker image centered over it. When the location is chosen, place a real marker and hide the fake one.
If the "pointy end" of the fake marker image is not in its exact center, simply pad the image with transparent space until it is.
Related Topics
How to Run a Specific Android App Using Terminal
Release APK Not Updating with JavaScript Code
Successful Share Intent for Android
Linearlayout Height in Oncreate Is 0
Android Studio Could Not Find Any Version That Matches Com.Android.Support:Appcompat-V7:+
Notificationlistenerservice Implementation
Rejecting Incoming Call in Android
Android View Layout_Width - How to Change Programmatically
How to Hide The System/Navigation Bar in Android Ics
Change Background of Edittext's Error Message
How to Get The Time Spent on an Application in Android Programmatically
Adb Shell Su Works But Adb Root Does Not
Items Inside Gridview Getting Repeated When Screen Scrolls
How to Take Pictures from The Camera Without Preview When My App Starts
How to Add a View Programmatically to Relativelayout
Bitmapfactory.Decodestream Out of Memory Despite Using Reduced Sample Size