0

I have a MapActivity that is meant to display a specific region, defined by instance variables mCenterPoint, mLatSpan and mLongSpan. The Map object has methods to get the center point, as well as the latitude span and longitude span of the map. When the MapActivity is opened, I want it to immediately center on the center point and zoom to the correct span.

Here is my onCreate method and my setRegionOfMap method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.google_map);

    mMapView = (MapView) findViewById(R.id.mapview);
    mMapView.setBuiltInZoomControls(true);

    setRegionOfMap();

}
private void setRegionOfMap(){
    mMapView.getController().setCenter(mCenterPoint);
    mMapView.getController().zoomToSpan(mLatSpan, mLongSpan);
}

mMap is just an instance of my map object. The center point and spans are always the same value since the data does not change dynamically. About 1/4 - 1/2 of the time the map is zoomed exactly as I expect it to. Yet the rest of the time the map looks centered correctly, except way too far zoomed out. Why would the MapView only zoom some of the time and other times be incorrect? My only theory was that maybe the map isn't totally drawn when the zoomToSpan call occurs, and thus the zoom doesn't register. I haven't been able to test that theory though.

mattgmg1990
  • 5,576
  • 4
  • 21
  • 26

2 Answers2

1

I ended up solving this on my own by calling zoomToSpan from an OnGlobalLayoutListener. The problem before was that in onCreate(), the map had not been drawn yet, so the zoom had no effect. Calling zoomToSpan from this listener makes sure the map is set up and the zoom has an effect. Here is my "setRegionOfMap()" method.

    private void setRegionOfMap(){
    final LinearLayout layout = (LinearLayout)findViewById(R.id.outdoor_maps_linearlayout);
    final ViewTreeObserver vto = layout.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            MapView mapView = (MapView)layout.findViewById(R.id.mapview);
            mapView.getController().setCenter(mCenterPoint);
            mapView.getController().zoomToSpan(mLatSpan, mLongSpan);
        } 
    });
}
mattgmg1990
  • 5,576
  • 4
  • 21
  • 26
  • @IgorGanapolsky I'm not sure. I am still using this code in production, but we haven't upgraded to v2 of the maps API for Android yet. – mattgmg1990 Dec 17 '12 at 22:10
0

I think the problem is that mMap.getLatSpan() and mMap.getLongSpan() resturns 0 if you call the method just at the start onCreate, If you calculate in a postdelay runnable it has to work.

here you can find better answer:

Mapview getLatitudeSpan and getLongitudeSpan not working

code I use to center and zoom between two locations:

if (searchedLocationFrom != null && searchedLocationTo != null) { // center

            int lat1 = (int) (searchedLocationFrom.getLatitude() * 1E6);
            int lon1 = (int) (searchedLocationFrom.getLongitude() * 1E6);
            int lat2 = (int) (searchedLocationTo.getLatitude() * 1E6);
            int lon2 = (int) (searchedLocationTo.getLongitude() * 1E6);

            // mapController.setZoom(13);
            mapController.zoomToSpan((lat1 > lat2 ? lat1 - lat2 : lat2
                    - lat1), (lon1 > lon2 ? lon1 - lon2 : lon2 - lon1));
            int z = mapView.getZoomLevel();
            mapController.setZoom(z > 1 ? z - 1 : z);
            mapController
                    .setCenter(new GeoPoint(lat1 - ((lat1 - lat2) / 2),
                            lon1 - ((lon1 - lon2) / 2)));
        }

Hope to help :)

Community
  • 1
  • 1
Litus
  • 632
  • 6
  • 11
  • I'm sorry, I think the name for my custom class Map is confusing. in that line 'mMap.getLatSpan()' is not querying the mapView, it is querying an object of a custom type that I created. I wrote the method "getLatSpan()" and "getLongSpan()" and I know they are returning the correct value. The problem is with the zoomToRegion call. I will edit my question code to be less confusing. – mattgmg1990 Jul 17 '12 at 19:12
  • ok, I don't see the zoomToRegion method. Post all the code and I can try to help – Litus Jul 17 '12 at 19:20
  • Sorry again, that was not the name, I meant zoomToSpan. The line that isn't working is `mMapView.getController().zoomToSpan(mLatSpan, mLongSpan);` That method is provided by the mapViewController class and I'm finding that it works only half of the time. – mattgmg1990 Jul 17 '12 at 19:22
  • ok, I understand, please show me the code on how and where you retrieve the values mLatSpan and mLongSpan and I can understand more the issue. I think that when you obtain the values mLatSpan and mLongSpan sometimes are 0, and when you debug the values always return correct values because the time you obtain the values are bigger than when it's executed normally. Try to put a postdelay when you retrieve the mLatSpan or mLongSpan or show the values of these variables on the logcat to see if sometimes is 0. – Litus Jul 17 '12 at 20:00
  • I have a class that reads in data from a json file on Amazon S3 that never changes. The data that I read in contains lat/long values for four corners of a rectangular region on earth that I want my map to show. In order to get the lat and long span my methods just do some math on the values for the corners of the rectangle. `public int getLongSpan(){ return (int)((this.mTopRightCoordinate.x - this.mTopLeftCoordinate.x) * 1E6) / 2; } public int getLatSpan(){ return (int)((this.mTopRightCoordinate.y - this.mBottomRightCoordinate.y) * 1E6) / 2; }` – mattgmg1990 Jul 17 '12 at 20:20
  • Hi, you are right. Sorry, but I'm also not understangin why is not working. I edit my answer and put the code I use to center the map and zoom between two coordinates and hope you can change it to adapt in your code. – Litus Jul 18 '12 at 08:03
  • Also the description of zoom to span says that attempt to adjust the zoom of the map: https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapController#zoomToSpan(int, int) – Litus Jul 18 '12 at 08:07