【问题标题】:Change Camera Zoom based on Radius Google Maps iOS SDK基于 Radius Google Maps iOS SDK 更改相机缩放
【发布时间】:2014-09-30 07:25:22
【问题描述】:

我正在开发一个应用程序,该应用程序会根据您当前位置周围的半径显示某些标记。半径在 100 - 5000 米之间。我用UISlider 更改半径并重新绘制GMSCircle

我的问题是我想根据滑块值更新相机变焦,但我不知道按哪个比例划分。

这就是我在 viewDidLoad 方法中创建相机的方式,其中初始缩放值为 15:

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude  zoom:15];

这是我正在做的工作的屏幕截图。

有谁知道我应该使用什么比例来相应地移动缩放?

非常感谢!

花岗岩

【问题讨论】:

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


    【解决方案1】:

    这是获取GMSCircle 边界的更简单解决方案。 它不依赖 MapKit,避免了改变相机位置的两个调用(moveCameraanimateToLocation

    import GoogleMaps
    
    extension GMSCircle {
        func bounds () -> GMSCoordinateBounds {
            func locationMinMax(_ positive : Bool) -> CLLocationCoordinate2D {
                let sign: Double = positive ? 1 : -1
                let dx = sign * self.radius  / 6378000 * (180 / .pi)
                let lat = position.latitude + dx
                let lon = position.longitude + dx / cos(position.latitude * .pi / 180)
                return CLLocationCoordinate2D(latitude: lat, longitude: lon)
            }
    
            return GMSCoordinateBounds(coordinate: locationMinMax(true),
                                   coordinate: locationMinMax(false))
        }
    }
    

    将此文件添加到您的项目后,您所要做的就是:

    let update = GMSCameraUpdate.fit(myCircle.bounds())
    myMap.animate(with: update)
    

    其中myCirclemyMap 被实际的圆圈和地图替换。

    【讨论】:

    • 效果很好! :)
    • 非常感谢 :-)
    • @Vlad Lego 你能解释一下为什么你使用了 6378000 常量吗?
    • @Vignesh 6378000 是“以米为单位的绕地球的圆的半径”。
    • @Vlad Lego 你能在 Objective-c 中给出这个答案吗?
    【解决方案2】:

    在 Saxon Druce 的帮助下,我终于做到了。

    class MapUtil {
    
    class func translateCoordinate(coordinate: CLLocationCoordinate2D, metersLat: Double,metersLong: Double) -> (CLLocationCoordinate2D) {
        var tempCoord = coordinate
    
        let tempRegion = MKCoordinateRegionMakeWithDistance(coordinate, metersLat, metersLong)
        let tempSpan = tempRegion.span
    
        tempCoord.latitude = coordinate.latitude + tempSpan.latitudeDelta
        tempCoord.longitude = coordinate.longitude + tempSpan.longitudeDelta
    
        return tempCoord
    }
    
    class func setRadius(radius: Double,withCity city: CLLocationCoordinate2D,InMapView mapView: GMSMapView) {
    
        let range = MapUtil.translateCoordinate(city, metersLat: radius * 2, metersLong: radius * 2)
    
        let bounds = GMSCoordinateBounds(coordinate: city, coordinate: range)
    
        let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 5.0)    // padding set to 5.0
    
        mapView.moveCamera(update)
    
        // location
        let marker = GMSMarker(position: city)
        marker.title = "title"
        marker.snippet = "snippet"
        marker.flat = true
        marker.map = mapView
    
        // draw circle
        let circle = GMSCircle(position: city, radius: radius)
        circle.map = mapView
        circle.fillColor = UIColor(red:0.09, green:0.6, blue:0.41, alpha:0.5)
    
        mapView.animateToLocation(city) // animate to center
    }
    

    }

    【讨论】:

      【解决方案3】:

      您可以使用GMSCameraUpdatefitBounds 方法,传入一个GMSCoordinateBounds,它是从您的圆的边缘计算出来的。

      基于this answer,您似乎可以使用MKCoordinateRegionMakeWithDistance 将您的中心(纬度/经度)加上半径(米)转换为MKCoordinateRegion,这会将米转换为以度为单位的跨度,因此允许您计算用于创建GMSCoordinateBounds 的度数坐标。

      【讨论】:

        【解决方案4】:

        我刚刚为 GMSCameraUpdate 创建了扩展。

        输入参数:

        坐标 - 你的中心坐标

        radius - 可见边界的半径

        extension GMSCameraUpdate {
        
            static func fit(coordinate: CLLocationCoordinate2D, radius: Double) -> GMSCameraUpdate {
                var leftCoordinate = coordinate
                var rigthCoordinate = coordinate
        
                let region = MKCoordinateRegionMakeWithDistance(coordinate, radius, radius)
                let span = region.span
        
                leftCoordinate.latitude = coordinate.latitude - span.latitudeDelta
                leftCoordinate.longitude = coordinate.longitude - span.longitudeDelta
                rigthCoordinate.latitude = coordinate.latitude + span.latitudeDelta
                rigthCoordinate.longitude = coordinate.longitude + span.longitudeDelta
        
                let bounds = GMSCoordinateBounds(coordinate: leftCoordinate, coordinate: rigthCoordinate)
                let update = GMSCameraUpdate.fit(bounds, withPadding: -15.0)
        
                return update
            }
        
        }
        

        【讨论】:

          【解决方案5】:

          对于 Swift 3

          extension GMSCircle {
          var bounds: GMSCoordinateBounds {
              return [0, 90, 180, 270].map {
                  GMSGeometryOffset(position, radius, $0)
                  }.reduce(GMSCoordinateBounds()) {
                      $0.includingCoordinate($1)
              }
          }
          }
          

          用法:

          mapView.animate(with: .fit(circle.bounds))
          

          【讨论】:

          • 更简单,这应该是公认的答案!值得一提的是,您可以在.fit 上添加withPadding 参数来更改填充。默认是 64,把我的改成 16。谢谢!
          【解决方案6】:

          希望它会起作用。

          在视图控制器中初始化这个圈子

           var cirlce: GMSCircle!
          

          在viewdidload中

           cirlce = GMSCircle(position: camera.target, radius: 10000)
                  cirlce.fillColor = UIColor.red.withAlphaComponent(0.5)
                         cirlce.map = mapView
          

          调用googlemap的代表

           func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
                     print(position.zoom)
                  var zoom:Double = Double(position.zoom/4)
                  zoom = pow(10, zoom)
                  zoom = 1000000/zoom
                   cirlce.radius = zoom
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-03-15
            • 2015-11-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-12
            • 1970-01-01
            相关资源
            最近更新 更多