2013-05-07 18 views
13

Voglio impostare il livello di zoom della mappa per adattarlo a tutti i marker sulla mappa. Ho usato il seguente metodo come detto da molte persone, ma non funziona per me. Sta mostrando qualche altro punto.zoom per adattarsi a tutti i marker sulla mappa google maps v2

if (positions.size() > 0) { 

    final LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    for (Marker m : positions) { 
     builder.include(m.getPosition()); 
    } 
    builder.include(positions.get(i).getPosition()); 
} 

try { 
    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() { 

     @Override 
     public void onCameraChange(CameraPosition arg0) { 

      googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
        builder.build(), UserDataManager.getInstance().getWidth(), 
        UserDataManager.getInstance().getWidth(),0)); 
      googleMap.setOnCameraChangeListener(null); 

     } 
    }); 

} catch (IllegalStateException e) { 
    // TODO: handle exception 
    final View mapView = getSupportFragmentManager() 
      .findFragmentById(R.id.map).getView(); 
    if (mapView.getViewTreeObserver().isAlive()) { 
     mapView.getViewTreeObserver().addOnGlobalLayoutListener(
      new OnGlobalLayoutListener() { 

       // We check which build version we are using. 
       @Override 
       public void onGlobalLayout() { 
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
         mapView.getViewTreeObserver() 
           .removeGlobalOnLayoutListener(
             this); 
        } else { 
         mapView.getViewTreeObserver() 
           .removeOnGlobalLayoutListener(
             this); 
        } 

        googleMap.setOnCameraChangeListener(new OnCameraChangeListener() { 

         @Override 
         public void onCameraChange(CameraPosition arg0) { 

          googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
            builder.build(), 
            UserDataManager.getInstance().getWidth(), 
            UserDataManager.getInstance().getWidth(),0)); 
          googleMap.setOnCameraChangeListener(null); 

         } 
        }); 

       } 
      }); 
     } 
    } 
} 
+0

Grazie per la movimentazione IllegalStateException :) – Hazlo8

risposta

45

Provare a utilizzare questa

//Calculate the markers to get their position 
LatLngBounds.Builder b = new LatLngBounds.Builder(); 
for (Marker m : markers) { 
    b.include(m.getPosition()); 
} 
LatLngBounds bounds = b.build(); 
//Change the padding as per needed 
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 25,25,5); 
mMap.animateCamera(cu); 

Scegli questa Google Maps v2 library che ho creato che include questa funzionalità basta usare

Marker m1,m2, m3, m4 .... ; 

mapHelper.zoomToFitMarkers(m1,m2, m3, m4 ....); 

EDIT:

ottenere la posizione corrente

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
//You can use GPS_PROVIDER also 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      currentLatitude = location.getLatitude(); 
      currentLongitude = location.getLongitude(); 
      //Add your marker here and zoom to fit it 
      //mapHelper.zoomToFitMarkers(m1,m2, m3, m4,mapHelper.addMarker(currentLatitude,currentLongitude,"Current Location");); 
     } 
    }); 
+0

sto ottenendo lo stesso risultato :( – Sharmilee

+0

Questo dovrebbe funzionare. Basta non applicare hardcode a 5 pixel e utilizzare invece i pixel indipendenti dalla densità. –

+1

utilizzando questo codice ottengo un errore del tipo "la dimensione della mappa non dovrebbe essere 0" ... qualche idea? –

3
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 15)); 
+0

. Spero che funzionerà per tutti. –

Problemi correlati