views:

461

answers:

1

Hey guys,

i've got 2 GeoPoints given to show them on the map with markers... so far so good...

how can I get the optimum zoom level for the MapController in order to focus the middle of both points, but also have them on the map.

The whole thing should work at different screen resolutions.

Sorry for asking that silly question, I know thats not very difficult, but at the moment my head is boiling :/

+2  A: 

You can do this using the MapView's MapController. You get the MapController using MapView.getController(), then you can attempt to set the correct zoom using MapController.zoomToSpan(). I also tend to use ItemizedOverlay's getLatSpanE6() and getLonSpanE6() to make this simpler. An example:

MapView map = (MapView) findViewById(R.id.Map);
MapController mc = map.getController();
mc.zoomToSpan(overlay.getLatSpanE6(), overlay.getLonSpanE6());

It is important to note their caveat:

Because the zoom can only achieve discrete levels, and because the aspect ratio of the map may not match the ratio given, the quality of the fit may vary. The only thing we guarantee is that, after the zoom, at least one of the new latitude or the new longitude will be within a factor of 2 from the corresponding parameter.

Edit: In order to also get the map to zoom on the correct place, you have to use MapController.setCenter().

Daniel Lew
in this case the map would zoom to a random position, wouldn't it?
poeschlorn
Both point will always be visible using this method. However, the further the points are from each other the less accurate this method becomes.
Dave.B
@poeschlorn: Correct, you also have to use setCenter(), sorry.
Daniel Lew
Thanks a lot :) It works fine!Now I just have to build in my marker, but that schould not be that difficult ;-) Thanks again!
poeschlorn