Google Maps SDK iOS - 根据缩放级别计算半径
Google Maps SDK iOS - Calculate radius according zoom Level
我需要计算半径以根据相机缩放级别在地图上显示标记。现在我有 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
}
而不是 southWest
,您应该使用 near/far
+Left/Right
。参考GMSVisibleRegion Struct Reference。原因是 southWest 不涵盖 3D 地图情况。
得到farRight、farLeft、nearRight、nearLeft后,就可以计算出两条水平边的两个中点之间的距离,以及垂直边的距离。然后你选择较小的值,这应该是你的半径。
好的,我已经为我的问题找到了很好的答案。也许它对其他人有帮助。根据这个 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)
}
更新反义词对 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()
会 return 半径
您可以使用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...
}
我需要计算半径以根据相机缩放级别在地图上显示标记。现在我有 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
}
而不是 southWest
,您应该使用 near/far
+Left/Right
。参考GMSVisibleRegion Struct Reference。原因是 southWest 不涵盖 3D 地图情况。
得到farRight、farLeft、nearRight、nearLeft后,就可以计算出两条水平边的两个中点之间的距离,以及垂直边的距离。然后你选择较小的值,这应该是你的半径。
好的,我已经为我的问题找到了很好的答案。也许它对其他人有帮助。根据这个 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)
}
更新反义词对 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()
会 return 半径
您可以使用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...
}