Google Maps SDK iOS - 根据缩放级别计算半径
Posted
技术标签:
【中文标题】Google Maps SDK iOS - 根据缩放级别计算半径【英文标题】:Google Maps SDK iOS - Calculate radius according zoom Level 【发布时间】:2015-11-03 19:07:56 【问题描述】:我需要根据相机缩放级别计算半径以在地图上显示标记。现在我有 SouthWestCorner 和我的位置是我的 MapView 的中心。当缩放改变时,我需要缩小并计算新的半径。
有人知道如何从我拥有的数据中获取它吗?
我的代码在这里:
func mapView(mapView: GMSMapView!, idleAtCameraPosition position: GMSCameraPosition!)
println("latitude: \(position.target.latitude) longtitude: \(position.target.longitude)")
var visibleRegion = mapView.projection.visibleRegion()
var cameraZoom = mapView.camera.zoom
var bounds = GMSCoordinateBounds(region: visibleRegion)
var southWestCorner = bounds.southWest
【问题讨论】:
【参考方案1】:您可以使用mapView.projection.visibleRegion()
查找可见屏幕上的角坐标。
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition)
// Find the coordinates
let visibleRegion = mapView.projection.visibleRegion()
let farLeftLocation = CLLocation(latitude: visibleRegion.farLeft.latitude, longitude: visibleRegion.farLeft.longitude)
let centerLocation = CLLocation(latitude: position.target.latitude, longitude: position.target.longitude)
// Calculate the distance as radius.
// The distance result from CLLocation is in meters, so we divide it by 1000 to get the value in kilometers
let radiusKM = centerLocation.distance(from: farLeftLocation) / 1000.0
// Do something with the radius...
【讨论】:
【参考方案2】:将 Antons anwser 更新为 Swift 3 并作为 GMSMapView
的扩展
extension GMSMapView
func getCenterCoordinate() -> CLLocationCoordinate2D
let centerPoint = self.center
let centerCoordinate = self.projection.coordinate(for: centerPoint)
return centerCoordinate
func getTopCenterCoordinate() -> CLLocationCoordinate2D
// to get coordinate from CGPoint of your map
let topCenterCoor = self.convert(CGPoint(x: self.frame.size.width, y: 0), from: self)
let point = self.projection.coordinate(for: topCenterCoor)
return point
func getRadius() -> CLLocationDistance
let centerCoordinate = getCenterCoordinate()
let centerLocation = CLLocation(latitude: centerCoordinate.latitude, longitude: centerCoordinate.longitude)
let topCenterCoordinate = self.getTopCenterCoordinate()
let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)
let radius = CLLocationDistance(centerLocation.distance(from: topCenterLocation))
return round(radius)
调用self.map.getRadius()
将返回半径
【讨论】:
【参考方案3】:好的,我已经为我的问题找到了很好的答案。也许它对其他人有帮助。根据这个article
要获得半径应该使用下一个示例(所有函数都转换为 swift):
// calculate radius
func getCenterCoordinate() -> CLLocationCoordinate2D
var centerPoint = self.mapView.center
var centerCoordinate = self.mapView.projection.coordinateForPoint(centerPoint)
return centerCoordinate
func getTopCenterCoordinate() -> CLLocationCoordinate2D
// to get coordinate from CGPoint of your map
var topCenterCoor = self.mapView.convertPoint(CGPointMake(self.mapView.frame.size.width / 2.0, 0), fromView: self.mapView)
var point = self.mapView.projection.coordinateForPoint(topCenterCoor)
return point
func getRadius() -> CLLocationDistance
var centerCoordinate = getCenterCoordinate()
// init center location from center coordinate
var centerLocation = CLLocation(latitude: centerCoordinate.latitude, longitude: centerCoordinate.longitude)
var topCenterCoordinate = self.getTopCenterCoordinate()
var topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)
var radius = CLLocationDistance(centerLocation.distanceFromLocation(topCenterLocation))
return round(radius)
【讨论】:
【参考方案4】:您应该使用near/far
+Left/Right
,而不是southWest
。请参阅GMSVisibleRegion Struct Reference。原因是西南不包括 3D 地图的情况。
得到farRight、farLeft、nearRight、nearLeft后,可以计算出两条水平边的两个中点之间的距离,以及垂直边的距离。然后你选择较小的值,这应该是你的半径。
【讨论】:
谢谢,有例子或公式吗? 我认为谷歌地图有计算两点之间距离的功能。此外,由于 lat/lng 是度数,而中点只是[ (FL.lat+NL.lat)/2, (FL.lng + NL.lng)/2 ]
和 [ (FR.lat+NR.lat)/2, (FR.lng + NR.lng)/2 ]
。以上是关于Google Maps SDK iOS - 根据缩放级别计算半径的主要内容,如果未能解决你的问题,请参考以下文章
google-maps-sdk-ios - 适用于 iOS 的 Google 地图 1.4.3 的 arm 7/iphone 5 问题
iOS 中使用 nativescript-google-maps-sdk 的地图中心错误