2013-02-28 9 views

risposta

56

Ecco la mia soluzione per questo problema. Costruire un oggetto GMSCoordinateBounds con più coordinate.

- (void)focusMapToShowAllMarkers 
{  
    CLLocationCoordinate2D myLocation = ((GMSMarker *)_markers.firstObject).position; 
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation]; 

    for (GMSMarker *marker in _markers) 
     bounds = [bounds includingCoordinate:marker.position]; 

    [_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:15.0f]]; 
} 

Aggiornato risposta: Dal GMSMapView marcatori proprietà è deprecato, si dovrebbe salvare tutti i marcatori nel proprio array.

aggiornato rapida 3 risposta:

func focusMapToShowAllMarkers() { 
     let firstLocation = (markers.first as GMSMarker).position 
     var bounds = GMSCoordinateBoundsWithCoordinate(firstLocation, coordinate: firstLocation) 

     for marker in markers { 
      bounds = bounds.includingCoordinate(marker.position) 
     } 
     let update = GMSCameraUpdate.fitBounds(bounds, withPadding: CGFloat(15)) 
     self.mapView.animate(cameraUpdate: update) 
    } 
+2

funziona bene. Sebbene la proprietà dell'indicatore sia stata deprecata ora. Io invece ho usato le posizioni dei marker che avevo in un array. –

+0

Grazie, ho usato questa ottima soluzione :) ma come ha detto @ManishAhuja, ho usato il mio array GMSMarker memorizzato per costruire i limiti. –

+0

Grazie a @ManishAhuja e AubadaTaijo. Ho aggiornato la risposta. – Lirik

3

Per ora Google ha finalmente implementato GMSCoordinateBounds, che è possibile utilizzare con GMSCameraUpdate.

Per ulteriori dettagli, consultare lo reference ufficiale.

5

Swift 3.0 versione di risposta di Lirik:

func focusMapToShowAllMarkers() { 
    let myLocation: CLLocationCoordinate2D = self.markers.first!.position 
    var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: myLocation, coordinate: myLocation) 

    for marker in self.markers { 
     bounds = bounds.includingCoordinate(marker.position) 
     self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0)) 
    } 
} 

Ed ecco la mia strada:

func focusMapToShowMarkers(markers: [GMSMarker]) { 

    guard let currentUserLocation = self.locationManager.location?.coordinate else { 
     return 
    } 

    var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: currentUserLocation, 
                  coordinate: currentUserLocation) 

    _ = markers.map { 
     bounds = bounds.includingCoordinate($0.position) 
     self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0)) 
    } 
} 

e si può chiamare il mio funzione sopra in questo modo:

self.focusMapToShowMarkers(markers: [self.myLocationMarker, currentPokemonMarker])

Problemi correlati