Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center the camera so that marker is at the bottom of screen? (Google map api V2 Android)

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 ^ ^

like image 691
Arch1tect Avatar asked May 26 '13 21:05

Arch1tect


People also ask

What is the position of the camera in a map?

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.

What are Google markers?

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.

How to zoom in Google map see on underlying page?

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.

How do you drag a marker on Google Maps?

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.


1 Answers

I might edit this answer later to provide some code, but what I think could work is this:

  1. Get LatLng (LatLng M) of the clicked marker.
  2. Convert LatLng M to a Point (Point M) using the Projection.toScreenLocation(LatLng) method. This gives you the location of the marker on the device's display (in pixels).
  3. Compute the location of a point (New Point) that's above Point M by half of the map's height.
  4. 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); 
like image 104
zbr Avatar answered Oct 07 '22 06:10

zbr