简体   繁体   中英

Cannot zoom above whole country, java Google Maps V2

I am developing a project with a map displaying 100 markers in a country (Greece). When my map is open while launching application in my mobile android phone, map zooms in Africa and in a tablet device it zooms in different location- country. This happens when my location is disabled through settings of my device. How can I set the map to zoom above whole country Greece when my map is loaded for first time? in any android device application is installed?

Please check my code below:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();

}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

/**
 * Check mMap. In case it is not null then setUpMap
 */
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

When I activate current location then it zooms on level 14 over my location. I need to keep this functionality when location setting are enabled.

// Show the current location in Google Map
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

            // Zoom in the Google Map
            mMap.animateCamera(CameraUpdateFactory.zoomTo(14));

You can do:

map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );

Where the coords, are for example the "middle" of Greece. And you will need to set some SharedPreference to know if is the first time that the app is open.

You can use the new version of API that calls you when the map is loaded: http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback.html You can easily check if it is the first time by using a simple "singleton" or static variable.

About the Zoom, I would recomend you to build a LatLngBounds and zoom on it. You can do this by iterating over all the Markers:

LatLngBounds.Builder zoomTo = LatLngBounds.builder();
for (Marker marker : markersList) {
     zoomTo.include(marker.getPosition());
}
final LatLngBounds workArea = zoomTo.build();
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
     @Override
     public void onMapLoaded() {
           mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(workArea, 50));
           mMap.setOnMapLoadedCallback(null);
         }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM