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

iOS,如何使用GMSCoordinateBounds显示地图的所有标记?,ios,google-maps,google-maps-sdk-ios,Ios,Google Maps,Google Maps Sdk Ios,我想显示我地图上的所有标记,在做了一些搜索后,我发现应该使用GMSCoordinateBounds(谷歌地图SDK)来完成。我已经阅读了有关它的官方文档,但我不知道如何使用它并在代码中实现它 这是我的密码 GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; CLLocationCoordinate2D location; for (NSDictionary *dictionary in array) {

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

这是我的密码

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

有什么帮助吗?

您可以迭代地图标记,获得东、西、北和南的进一步点,并创建
[[GMSCoordinateBounds alloc]initWithRegion:

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


}
这是我的结果:

@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))

@IBOutlet弱var映射视图:GMSMapView!
让摄像机=GMSCameraPosition.cameraWithLatitude(23.0793,经度:
72.4957,缩放:5)
mapView.camera=camera
mapView.delegate=self
mapView.myLocationEnabled=true
***arry有dictionary对象,该对象的值为纬度和经度***
let path=GMSMutablePath()

对于0..中的i,我找到的最简单的方法是创建一个GMSMutablePath,然后将标记的所有坐标添加到其中。然后可以使用GMSCoordinateBounds初始值设定项initWithPath:创建边界

一旦有了边界,就可以创建GMSCameraUpdate,并使用它将贴图设置为包含所有标记的可见边界

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

对于Google Maps 2.0.0版,如果您尝试使用默认构造函数GMSCoordinateBounds()创建GMSCoordinateBounds,并选中“valid”标志,它将返回false,并且不会使animateWithCameraUpdate:move

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))
    }

使用不带路径的
GMSCoordinateBounds()
快速解决方案

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)

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)

清洁swift 3版本:

let bounds = markers.reduce(GMSCoordinateBounds()) { 
    $0.includingCoordinate($1.position) 
}

mapView.animate(with: .fit(bounds, withPadding: 30.0))

我们可以在每个地方找到相同的代码,但是要确保它在viewdideappease()方法中

//bounding to a region of markers
GMSCoordinateBounds *bounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:sourceMarker.position coordinate:destMarker.position];
[_mapView moveCamera:[GMSCameraUpdate fitBounds:bounds withPadding:50.0]];
根据,gmscoordinatedbounds不能突变

GMSCoordinateBounds是不可变的,在构造后不能修改

因此,我们可以使用上述方法对GMSCoordinateBounds进行变异,包括坐标:(用于扩展坐标)和边界(用于扩展边界)

因此,Swift代码如下所示:

var gmsBounds = GMSCoordinateBounds()
for coordinate in coordinates {
    let marker = GMSMarker(position: coordinate)
    gmsBounds = gmsBounds.includingCoordinate(position)
    marker.map = mapView
}
mapView.animate(with: GMSCameraUpdate.fit(gmsBounds, withPadding: 30.0))
补充说明:


如果您添加自定义标记视图,除了所需的填充之外,还可以添加填充作为标记视图的宽度。

谢谢您的回答,请您更具体一点,因为我是一个新手,一段代码片段将非常有助于清楚地理解这一想法。它具有非常大的缩小范围!它显示了世界地图:我认为你做错了什么,在我的应用程序中运行得很好,请发布你的代码。我刚刚更新了我的帖子,并包含了我使用的代码,请检查。@Al_fareS我在viewDidLoad中尝试了allemattio代码,结果显示为非常大的缩小。它显示了世界地图。但后来,我将代码移动到ViewDidDisplay,正如allemattio实际建议的那样,结果看起来很好,就像allemattio的屏幕截图一样。对,将此代码移动到ViewDisplay将解决“世界大小比例”的问题。请您将我的答案标记为已接受?如果所有PIN都没有显示在地图视图上,检查您是否未限制mapView最小和最大缩放。因此,如果您发现地图缩小到最大,请确保mapView.frame设置正确。感谢
mapView.frame
tip@AliAmin-我有一个文档中建议的
.zero
帧,它没有缩放!是的,您必须确保您的地图视图已经是布局。最好在viewDidLayoutSubviews回调中调用它。这不是这个问题的答案,但当然很有用。
- (GMSCoordinateBounds *)includingCoordinate:(CLLocationCoordinate2D)coordinate;
- (GMSCoordinateBounds *)includingBounds:(GMSCoordinateBounds *)other;
var gmsBounds = GMSCoordinateBounds()
for coordinate in coordinates {
    let marker = GMSMarker(position: coordinate)
    gmsBounds = gmsBounds.includingCoordinate(position)
    marker.map = mapView
}
mapView.animate(with: GMSCameraUpdate.fit(gmsBounds, withPadding: 30.0))