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

前端 未结 10 2136
执念已碎
执念已碎 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:30

    I prefer Larry McKenzie's answer which it doesn't depend on screen projection (i.e. mProjection.toScreenLocation()), my guess is the projection resolution will go poor when the map zoom level is low, it made me sometimes couldn't get an accurate position. So, calculation based on google map spec will definitely solve the problem.

    Below is an example code of moving the marker to 30% of the screen size from bottom.

    zoom_lvl = mMap.getCameraPosition().zoom;
    double dpPerdegree = 256.0*Math.pow(2, zoom_lvl)/170.0;
    double screen_height = (double) mapContainer.getHeight();
    double screen_height_30p = 30.0*screen_height/100.0;
    double degree_30p = screen_height_30p/dpPerdegree;      
    LatLng centerlatlng = new LatLng( latlng.latitude + degree_30p, latlng.longitude );         
    mMap.animateCamera( CameraUpdateFactory.newLatLngZoom( centerlatlng, 15 ), 1000, null);
    

提交回复
热议问题