更改基于Radius Google Maps iOS SDK的相机缩放
我正在开发一个应用程序,可以根据当前位置周围的半径来显示某些标记。 半径在100-5000米之间。 我用UISlider
更改半径并重绘GMSCircle
。
我的问题是,我想根据滑块值更新相机变焦,但我不知道哪个比例分割。
这是我如何在viewDidLoad
方法中创build初始缩放值为15的相机:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude zoom:15];
这是我正在工作的屏幕截图。
有谁知道应该用什么比例来相应地移动缩放?
非常感谢!
格兰尼特
在撒克逊德鲁士的帮助下,终于做到了。
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 }
}
这里有一个简单的解决scheme来获取GMSCircle
的边界。 它不依赖于MapKit,避免了两个改变摄像机位置的调用( moveCamera
和animateToLocation
)
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/M_PI) let lat = position.latitude + dx let lon = position.longitude + dx / cos(position.latitude * M_PI/180) return CLLocationCoordinate2D(latitude: lat, longitude: lon) } return GMSCoordinateBounds(coordinate: locationMinMax(true), coordinate: locationMinMax(false)) } }
在将这个文件添加到你的项目之后,你所要做的就是:
let update = GMSCameraUpdate.fitBounds(myCircle.bounds()) myMap.animateWithCameraUpdate(update)
myCircle
和myMap
被实际的圆和地图取代。
你可以使用GMSCameraUpdate
的fitBounds
方法,传递一个GMSCoordinateBounds
,它是从你的圆的边缘计算出来的。
基于这个答案 ,看起来你可以使用MKCoordinateRegionMakeWithDistance
将你的中心(纬度/经度)加上半径(米)转换成一个MKCoordinateRegion
坐标区域,它将把米转换成一个以度为单位的跨度,因此允许你计算坐标您将使用它来创buildGMSCoordinateBounds
。
对于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))
我刚刚为GMSCameraUpdate创build了扩展。
input参数:
坐标 – 你的中心坐标
半径 – 可见边界的半径
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 } }
- 例外情况:在使用之前,Google Maps SDK for iOS必须通过进行初始化
- 当我在Google Maps SDK for iOS中点击另一个标记时,是否可以拦截标记?
- 如何在iOS中的Google地图上添加button?
- 在Google地图中点击标记时显示信息窗口在实现mapView时执行iOS sdk:didTapMarker:实现了delegate方法
- 使用未parsing的标识符'GMSServices'
- GoogleMaps iOS SDK 1.5将不会加载
- Google Maps iOS SDK崩溃
- Google地图标记不会移除iOS
- 如何从GMSMapView中删除GMSPolygon