【问题标题】:iOS, How to use GMSCoordinateBounds to show all the markers of the map?iOS,如何使用 GMSCoordinateBounds 显示地图的所有标记?
【发布时间】:2014-03-04 03:50:44
【问题描述】:

我想显示地图上的所有标记,在进行了一些搜索后,我发现应该使用 GMSCoordinateBounds(Google Maps 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]];

有什么帮助吗?

【问题讨论】:

  • 能否将我的回答标记为已接受?

标签: ios google-maps google-maps-sdk-ios


【解决方案1】:
- (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]];

这是我的结果:

【讨论】:

  • 它的缩小非常大!它显示了世界地图:s
  • 我认为你做错了什么,在我的应用中完美运行,请发布你的代码..
  • 我刚刚更新了我的帖子,并包含了我使用的代码,请检查它。
  • @Al_fareS 我在 viewDidLoad 中尝试了 allemattio 代码,结果出现了很大的缩小。它显示了世界地图。但是后来,我按照 allemattio 的实际建议将代码移动到 viewDidAppear,结果看起来很好,就像 allemattio 的屏幕截图一样。
  • 对,把这段代码移到 viewWillAppear 就解决了“世界大小比例”的问题
【解决方案2】:

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)

【讨论】:

  • 如果所有图钉都没有显示在 mapView 上,请检查您没有限制 mapView 的最小和最大缩放
  • 另外,如果您发现地图缩小到最大,请确保 mapView.frame 设置正确。
  • 感谢mapView.frame 提示@AliAmin - 我有一个.zero 的框架,正如文档中所推荐的那样,它没有缩放!
  • 对,您必须确保您的地图视图已经布局。最好在 viewDidLayoutSubviews 回调中调用它。
【解决方案3】:

干净的swift 3版本:

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

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

【讨论】:

    【解决方案4】:

    使用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)
    

    【讨论】:

      【解决方案5】:

      我发现最简单的方法是创建一个 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]];
      

      【讨论】:

        【解决方案6】:

        对于 Google 地图 2.0.0 版,如果您尝试使用默认构造函数 GMSCoordinateBounds() 创建 GMSCoordinateBounds 并检查“有效”标志,它将返回 false 并且不会使 animateWithCameraUpdate: 移动。

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

        【讨论】:

          【解决方案7】:
          @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))
          

          【讨论】:

            【解决方案8】:

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

            //bounding to a region of markers
            GMSCoordinateBounds *bounds =
            [[GMSCoordinateBounds alloc] initWithCoordinate:sourceMarker.position coordinate:destMarker.position];
            [_mapView moveCamera:[GMSCameraUpdate fitBounds:bounds withPadding:50.0]];
            

            【讨论】:

            • 它不是这个问题的答案,但当然有用。
            【解决方案9】:

            您可以迭代您的地图标记并获取更远的点到东、西、北和南并制作[[GMSCoordinateBounds alloc] initWithRegion:]

            【讨论】:

            • 感谢您的回答,由于我是新手,能否请您说的更具体一些,代码 sn-p 将非常有助于清楚地理解这个想法。
            【解决方案10】:

            根据documentationGMSCoordinateBounds 不能改变

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

            - (GMSCoordinateBounds *)includingCoordinate:(CLLocationCoordinate2D)coordinate;
            - (GMSCoordinateBounds *)includingBounds:(GMSCoordinateBounds *)other;
            

            所以我们可以通过使用上述方法包括Coordinate:(用于扩展坐标)和includeBounds:(用于扩展边界)来改变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))
            

            补充说明:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-30
              • 2020-01-23
              • 2011-01-30
              • 1970-01-01
              • 2011-11-02
              • 2015-11-20
              相关资源
              最近更新 更多