tricky Android Google Map zoom while maintaining center point -
it's not easy explaining problem try.
i have android googlemap, on top of it, have imageview positioned @ center @ times. if drag/pan map, pin in center of googlemap.
now, add marker, somewhere on map. want zoom such center point remains in center of map, , marker visible within map, , highest zoom level.
the problem if check if marker within boundaries of map or not, , keep zooming in/out till is, process repeat itself, i.e. trying zoom in , if marker became outside, zoom out.
the problem rely on oncamerachange listener keep calling everytime zoom in or out, hence, process of zooming in/out keep occuring indefinitely
journeygooglemap.setoncamerachangelistener(new oncamerachangelistener() { @override public void oncamerachange(final cameraposition position) {
basically, need function can provide center latlng , markerlatlng , automatically calculate latlngbounds making sure center within center of latlng bounds, , can use
public static cameraupdate newlatlngbounds (latlngbounds bounds, int width, int height, int padding)
as shown in link below https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/cameraupdatefactory#newlatlngbounds(com.google.android.gms.maps.model.latlngbounds,int, int,int)
if need anymore clarification please tell me
you need calculate latlngbounds
2 points:
- your marker position and
- place on opposite side of current center.
the second calculated this:
latlng other = new latlng(2 * center.latitude - position.latitude, 2 * center.longitude - position.longitude);
see latlngboundsutils.fromcenterandpositions general solution.
Comments
Post a Comment