iOS, Comment utiliser GMSCoordinateBounds pour afficher tous les marqueurs de la carte?

Je veux montrer tous les marqueurs qui sont sur ma carte, après avoir fait des searchs, j'ai trouvé que cela devrait être fait avec GMSCoordinateBounds (Google Maps SDK) J'ai lu la documentation officielle à ce sujet, mais je ne comprends pas comment l'utiliser et l'implémenter dans mon code.

https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_camera_update#aa5b05606808272f9054c54af6830df3e

Voici mon code

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; CLLocationCoordinate2D location; for (NSDictionary *dictionary in array) { location.latitude = [dictionary[@"latitude"] floatValue]; location.longitude = [dictionary[@"longitude"] floatValue]; // Creates a marker in the center of the map. GMSMarker *marker = [[GMSMarker alloc] init]; marker.icon = [UIImage imageNamed:(@"marker.png")]; marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude); bounds = [bounds includingCoordinate:marker.position]; marker.title = dictionary[@"type"]; marker.map = mapView_; } [mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]]; 

De l'aide ?

 - (void)focusMapToShowAllMarkers { GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; for (GMSMarker *marker in <An array of your markers>) bounds = [bounds includingCoordinate:marker.position]; [<yourMap> animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]]; } 

METTRE À JOUR:

êtes-vous sûr qu'il n'y a rien de mal dans votre tableau de marqueurs et les coordonnées? J'ai essayé ce code et fonctionne parfaitement. Je l'ai mis sur le viewDidAppear

 NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:[[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.33",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.453",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.44",@"latitude",@"21.993",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.635",@"latitude",@"21.553",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.3546",@"latitude",@"21.663",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.6643",@"latitude",@"21.212",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.63466",@"latitude",@"21.3523",@"longitude", nil],nil]; GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; CLLocationCoordinate2D location; for (NSDictionary *dictionary in array) { location.latitude = [dictionary[@"latitude"] floatValue]; location.longitude = [dictionary[@"longitude"] floatValue]; // Creates a marker in the center of the map. GMSMarker *marker = [[GMSMarker alloc] init]; marker.icon = [UIImage imageNamed:(@"marker.png")]; marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude); bounds = [bounds includingCoordinate:marker.position]; marker.title = dictionary[@"type"]; marker.map = mapView_; } [mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]]; 

C'est mon résultat:

carte avec marqueurs

Swift 3 – Xcode 8

 var bounds = GMSCoordinateBounds() for marker in yourArrayOfMarkers { bounds = bounds.includingCoordinate(marker.position) } let update = GMSCameraUpdate.fit(bounds, withPadding: 60) mapView.animate(with: update) 

Le moyen le plus simple que j'ai trouvé est de créer un GMSMutablePath et d'y append toutes les coordonnées de vos marqueurs. Vous pouvez ensuite utiliser l'initialiseur GMSCoordinateBounds initWithPath: pour créer les limites.

Une fois que vous avez les limites, vous pouvez créer GMSCameraUpdate et l'utiliser pour animer la carte aux limites visibles contenant tous vos marqueurs.

Par exemple, si vous avez un tableau de GMSMarker:

 NSArray *myMarkers; // array of marker which sets in Mapview GMSMutablePath *path = [GMSMutablePath path]; for (GMSMarker *marker in myMarkers) { [path addCoordinate: marker.position]; } GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path]; [_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]]; 

Solution GMSCoordinateBounds() utilisant GMSCoordinateBounds() sans path,

 var bounds = GMSCoordinateBounds() for location in locationArray { let latitude = location.valueForKey("latitude") let longitude = location.valueForKey("longitude") let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude:latitude, longitude:longitude) marker.map = self.mapView bounds = bounds.includingCoordinate(marker.position) } let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 100) mapView.animateWithCameraUpdate(update) 
 @IBOutlet weak var mapView: GMSMapView! let camera = GMSCameraPosition.cameraWithLatitude(23.0793, longitude: 72.4957, zoom: 5) mapView.camera = camera mapView.delegate = self mapView.myLocationEnabled = true *** arry has dictionary object which has value of Latitude and Longitude. *** let path = GMSMutablePath() for i in 0..<arry.count { let dict = arry[i] as! [Ssortingng:AnyObject] let latTemp = dict["latitude"] as! Double let longTemp = dict["longitude"] as! Double let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: latTemp, longitude: longTemp) marker.title = "Aussortinglia" marker.appearAnimation = kGMSMarkerAnimationNone marker.map = self.mapView path.addCoordinate(CLLocationCoordinate2DMake(latTemp, longTemp)) } let bounds = GMSCoordinateBounds(path: path) self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0)) 

En ce qui concerne Google Maps version 2.0.0, si vous essayez de créer un GMSCoordinateBounds en utilisant le constructor par défaut GMSCoordinateBounds () et que vous cochez l'option "valid", il renverra false et ne produira pas animateWithCameraUpdate: move.

Solution Swift 2.3

  if let myLocation = mapView.myLocation { let path = GMSMutablePath() path.addCoordinate(myLocation.coordinate) //add other coordinates //path.addCoordinate(model.coordinate) let bounds = GMSCoordinateBounds(path: path) mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 40)) } 

Clean swift 3 version:

 let bounds = markers.reduce(GMSCoordinateBounds()) { $0.includingCoordinate($1.position) } mapView.animate(with: .fit(bounds, withPadding: 30.0)) 

Vous pouvez itérer vos marqueurs de carte et get le point suivant vers l'est, l'ouest, le nord et le sud et faire [[GMSCoordinateBounds alloc] initWithRegion:]