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

前端 未结 10 2142
执念已碎
执念已碎 2020-11-30 23:13

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 conv

10条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 23:23

    Yes, use Projection class. More specifically:

    Get Projection of the map:

     Projection projection = map.getProjection();
    

    Get location of your marker:

     LatLng markerLocation = marker.getPosition();
    

    Pass location to the Projection.toScreenLocation() method:

     Point screenPosition = projection.toScreenLocation(markerLocation);
    

    Like this you can move your marker relative to center or around the screen

    Point mappoint = googleMap.getProjection().toScreenLocation(new LatLng(latitude, longitude));
            mappoint.set(mappoint.x, mappoint.y-30);
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(googleMap.getProjection().fromScreenLocation(mappoint)));
    

提交回复
热议问题