Sam Mendez
Sam Mendez

Reputation: 33

Android google maps zoom to fit mark and current position

I'm trying to create a custom "my location" button for my app using Google Maps.

What I'm trying to do is to center the map around the location of the user, which is already done and working flawlessly, but also want to zoom in just enough to see a marker of my choice (this is actually the closest of a set of markers I have in memory, but that's not important now).

I haven't been able to find how the zoom variable works here. If I know the marker I want to show is 0.5 GPS units away from me, how can I center the map around me in a way that includes that marker on its boundaries? I'd also use a padding to make sure it perfectly fits in the map.

LatLng my_coordinates = ...;
LatLng closest_mark = ...;

map.animateCamera(CameraUpdateFactory.newLatLng(my_coordinates));

So now I want to modify that code to not only center the position to my_coordinates but also make sure zoom will make closest_mark fit in the viewport

CameraUpdateFactory.newLatLngBounds( ?? , /*padding*/);

Upvotes: 3

Views: 1672

Answers (1)

Guru
Guru

Reputation: 916

I don't think there is a specific zoom variable in the api you can control along with the points in the map. (And LatLngBounds just takes in the upper right and lower left bounds and gets you a view accordingly).

I believe what you could do is with a little geometry. If your marker is very close to your location (you can consider it a rectangle), get the distance between the two and extrapolate that on the opposite direction with the same distance (multiple by a small factor if you want some padding) to get the other coordinates, and then you can get the upper right and lower left coordinates (simple geometry).

If your marker is quite far and the surface of the earth comes into picture, you may have to use the haversine formula (great circle distance).

Hope this helps.

Upvotes: 2

Related Questions