When a marker is clicked, the default behavior for the camera is to center it on screen, but because I usually have long text description in the info window, it's more convenient to actually change the camera position so that the marker is on the bottom of screen(making the info window in the center of screen). I think I should be able to do that by overriding onMarkerClick function like below (the default behavior is cancelled when this function return true)
@Override public boolean onMarkerClick(final Marker marker) { // Google sample code comment : We return false to indicate that we have not // consumed the event and that we wish // for the default behavior to occur (which is for the camera to move // such that the // marker is centered and for the marker's info window to open, if it // has one). marker.showInfoWindow(); CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(XXXX, XXXX)); mMap.moveCamera(center);//my question is how to get this center // return false; return true; }
Edit:
Problem solved using accepted answer's steps, codes below:
@Override public boolean onMarkerClick(final Marker marker) { //get the map container height LinearLayout mapContainer = (LinearLayout) findViewById(R.id.map_container); container_height = mapContainer.getHeight(); Projection projection = mMap.getProjection(); LatLng markerLatLng = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude); Point markerScreenPosition = projection.toScreenLocation(markerLatLng); Point pointHalfScreenAbove = new Point(markerScreenPosition.x, markerScreenPosition.y - (container_height / 2)); LatLng aboveMarkerLatLng = projection .fromScreenLocation(pointHalfScreenAbove); marker.showInfoWindow(); CameraUpdate center = CameraUpdateFactory.newLatLng(aboveMarkerLatLng); mMap.moveCamera(center); return true; }
Thanks for helping ^ ^
The position of the camera (and hence the rendering of the map) is specified by the following properties: target (latitude/longitude location) , bearing, tilt, and zoom. The camera target is the location of the center of the map, specified as latitude and longitude co-ordinates.
Markers identify locations on the map. The default marker uses a standard icon, common to the Google Maps look and feel. It's possible to change the icon's color, image or anchor point via the API. Markers are objects of type Marker, and are added to the map with the GoogleMap.addMarker(markerOptions) method. Markers are designed to be interactive.
We can set the situation of the camera and marker in any put on the earth. We can plan the marker as per our decision. It additionally accompanies a zoom property in a cameraposition to give the zooming in google map see on the underlying page.
To set this listener on the map, call GoogleMap.setOnMarkerDragListener. To drag a marker, a user must long press on the marker. When the user takes their finger off the screen, the marker will stay in that position. When a marker is dragged, onMarkerDragStart (Marker) is called initially.
I might edit this answer later to provide some code, but what I think could work is this:
LatLng
(LatLng M) of the clicked marker.Point
(Point M) using the Projection.toScreenLocation(LatLng)
method. This gives you the location of the marker on the device's display (in pixels).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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With