根据Radius Google Maps iOS SDK更改相机缩放

Gra*_*nit 9 iphone ios google-maps-sdk-ios

我正在开发一个应用程序,根据您当前位置的半径显示某些标记.半径在100到5000米之间.我用a改变半径UISlider并重新绘制GMSCircle.

我的问题是我想根据滑块值更新相机变焦,但我不知道要分割的比例.

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

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude  zoom:15];
Run Code Online (Sandbox Code Playgroud)

这是我正在做的工作的截图.

在此输入图像描述

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

非常感谢!

格兰尼特

Vla*_*ego 28

这是一个更简单的解决方案,用于获取a的界限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))
    }
}
Run Code Online (Sandbox Code Playgroud)

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

let update = GMSCameraUpdate.fit(myCircle.bounds())
myMap.animate(with: update)
Run Code Online (Sandbox Code Playgroud)

其中myCirclemyMap被实际的圆和地图所取代.


tou*_*bun 11

在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
}
Run Code Online (Sandbox Code Playgroud)

}在此输入图像描述


Sax*_*uce 5

您可以使用传入的fitBounds方法,该方法是根据圆的边缘计算出来的.GMSCameraUpdateGMSCoordinateBounds

基于这个答案,看起来您可以使用MKCoordinateRegionMakeWithDistance将您的中心(纬度/经度)加半径(米)转换为a MKCoordinateRegion,这会将米转换为以度为单位的跨度,因此您可以计算以度为单位的坐标用来创造GMSCoordinateBounds.