【问题标题】:Google Map radius to zoom level谷歌地图半径缩放级别
【发布时间】:2018-01-16 20:49:37
【问题描述】:

我有一张上面有自定义 UIView 的地图。我正在根据缩放级别计算圆的半径。

func getCenterCoordinate() -> CLLocationCoordinate2D {
    let centerPoint = self.mapView.center
    let centerCoordinate = self.mapView.projection.coordinate(for: centerPoint)
    return centerCoordinate
}

func getTopCenterCoordinate() -> CLLocationCoordinate2D {
    // to get coordinate from CGPoint of your map
    let topCenterCoor = self.mapView.convert(CGPoint(x:self.circleView.frame.size.width / 2.0, y:0), from: self.circleView)
    let point = self.mapView.projection.coordinate(for: topCenterCoor)
    return point
}

func getRadius() -> CLLocationDistance {

    let centerCoordinate = getCenterCoordinate()
    // init center location from center coordinate
    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)) / 1000
    print(radius)
    return round(radius)
}

现在,我想根据给定的半径缩放地图?我怎么能这样做?

【问题讨论】:

  • 您的地图是矩形的,那么您希望如何将圆形映射到矩形?一旦你决定了,你只需要打电话给mapView.setRegion()。
  • 请检查我的回答。希望对您有所帮助。

标签: ios swift google-maps


【解决方案1】:

在圆的周长上选择任何坐标,并将地图的缩放或相机的缩放设置为该坐标的边界,当然还要添加一些填充。现在对于这个逻辑,如果您的地图允许用户缩放,那么您每次地图的相机移动或重新定位时都必须重新计算。

您可以使用此方法:mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))

这里的 bounds 是一个 GMSCoordinateBounds 对象。

希望对你有帮助

【讨论】:

  • 感谢您的回答,但这并不是我真正想要的。例如,由于我在上面发布的那些功能,我在缩放级别 4 的半径为 923。我想要做的是找到我需要的缩放级别,例如半径为 10。
  • 这个答案会根据你的半径自动适合你的缩放,你不必明确给出缩放级别
猜你喜欢
  • 2017-10-09
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 2015-11-23
  • 2018-01-17
相关资源
最近更新 更多