iOS,如何使用GMSCoordinateBounds显示地图的所有标记?

我想显示在我的地图上的所有标记,做了一些search后,我发现它应该与GMSCoordinateBounds (谷歌地图SDK)完成我已阅读有关它的官方文档,但我不知道如何使用它并在我的代码中实现它。

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

这是我的代码

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]]; 

任何帮助?

 - (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]]; } 

更新:

你确定你的标记和坐标数组没有错吗? 我已经试过这个代码,并且工作正常。 我把这个放在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]]; 

这是我的结果:

与标记的地图

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) 

我find的最简单的方法是创build一个GMSMutablePath,然后添加标记的所有坐标。 然后您可以使用GMSCoordinateBounds初始化程序initWithPath:来创build边界。

一旦你有界限,你可以创buildGMSCameraUpdate并使用它来将地图设置为包含所有标记的可见边界。

例如,如果你有一个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]]; 

使用GMSCoordinateBounds()而不使用path的Swift解决scheme,

 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! [String: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 = "Austrilia" 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)) 

至于Google Maps版本2.0.0,如果您尝试使用默认构造函数GMSCoordinateBounds()创buildGMSCoordinateBounds,并且检查“valid”标志,它将返回false,并且不会进行animateWithCameraUpdate:move。

Swift 2.3解决scheme

  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版本:

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

你可以迭代你的地图标记,并得到东,西,北,南的进一步点,并使[[GMSCoordinateBounds alloc] initWithRegion:]