Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zoom a MapView so it always includes two geopoints?

I have two geo points that vary intermittently, and want the MapView to resize and translate to make sure both points are always visible. I can easily re-centre the map on the point mid-way between the two, but how do I set the zoom level to ensure my two points are visible?

like image 647
Ollie C Avatar asked Jan 19 '23 16:01

Ollie C


2 Answers

Check out his answer in a different post: Google Map V2 how to set ZoomToSpan?

It solves it without much hassle. Note that you can only use that function AFTER the map has been loaded at least once. If not use function with specified map size on screen

LatLngBounds bounds = new LatLngBounds.Builder()
                    .include(new LatLng(CURRENTSTOP.latitude, CURRENTSTOP.longitude))
                    .include(new LatLng(MYPOSITION.latitude, MYPOSITION.longitude)).build();

Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getSize(displaySize);

map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, displaySize.x, 250, 30));

Works like a charm and very dynamic!

like image 116
Alon Avatar answered Feb 20 '23 10:02

Alon


Try this and see:

double latitudeSpan = Math.round(Math.abs(firstLat - 
                secLat));
double longitudeSpan = Math.round(Math.abs(firstLong - 
                secLong));

double currentLatitudeSpan = (double)mapView.getLatitudeSpan();
double currentLongitudeSpan = (double)mapView.getLongitudeSpan();

double ratio = currentLongitudeSpan/currentLatitudeSpan;
if(longitudeSpan < (double)(latitudeSpan+2E7) * ratio){
    longitudeSpan = ((double)(latitudeSpan+2E7) * ratio);
}

mapController.zoomToSpan((int)(latitudeSpan*2), (int)(longitudeSpan*2));                
mapView.invalidate();
like image 26
Anju Avatar answered Feb 20 '23 10:02

Anju