3

I'm working in a Java Me application that uses google static maps. The problem is that I need to get the zoom and center that fits all markers, and I need to add some labels in some coordinates on the map. How can I do this? I need a function in the server or in the midlet to get this parameters, I can't use javascript.

gnat
  • 6,213
  • 108
  • 53
  • 73
Laura Isabel
  • 209
  • 4
  • 10

3 Answers3

7

I have had the same problem. Don't use "&zoom=xx" in your url, the map will auto-fit for all your markers!

Luca N.
  • 71
  • 1
  • 2
7

You don't need to calculate it on your own.

When you supply markers, usually the map will have a viewport to show all markers.

To modify the viewport use the visible-parameter.


Referring to the comments:
Pseudo-Code for calculating the center from a couple of Points:

latitude: (maxLatOfAllPoints+minLatOfAllPoints)/2
longitude:(maxLngOfAllPoints+minLngOfAllPoints)/2

krzysiej
  • 889
  • 9
  • 22
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • Thanks for your answer. The problem is that I need to get the center in the mobile application to be able to paint some labels in the map. – Laura Isabel Mar 05 '12 at 20:53
  • When you know the positions of the markers it needs a little bit mathematics to calculate the center(see above) – Dr.Molle Mar 06 '12 at 06:45
  • 1
    I came here searching for this, documentation URL should now be: https://developers.google.com/maps/documentation/static-maps/intro?hl=en#Viewports – tomvo Dec 18 '15 at 08:19
-1
  1. Define bounds object

    bounds = new google.maps.LatLngBounds();
    
  2. When you create and place marker on the map extend bondes with marker position.

    bounds.extend(mPosition);
    

    where mPosition is a google new google.maps.LatLng(0,0) object, the same that you use during marker creation.

  3. at the end ot you can create a link for click or just run.

    map.fitBounds(bounds);
    

That is it.

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38