谷歌地图iOS开发包 - 根据缩放级别计算半径

7

我需要计算半径,以根据相机缩放级别在地图上显示标记。现在我有南西角和我的位置,即我的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

    }
4个回答

19

好的,我已经找到了我的问题的好答案。也许这对其他人有帮助。根据这篇文章

获取��径应该使用下面的示例(所有函数都转换成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)
    }

7

更新 Anton 的答案以适应 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()会返回半径


0

你应该使用near/far+Left/Right而不是southWest。请参考GMSVisibleRegion Struct Reference。原因是southWest不能覆盖3D地图情况。

在获取farRightfarLeftnearRightnearLeft之后,你可以计算两个水平边缘的中点之间的距离以及垂直边缘的距离。然后您选择更小的值,这应该是您的半径。


谢谢,你有一些例子或公式吗? - Anton
我认为Google Maps有一个计算两点之间距离的功能。而且由于纬度/经度是以度数表示的,中间点可以简单地计算为[(FL.lat+NL.lat)/2, (FL.lng + NL.lng)/2][(FR.lat+NR.lat)/2, (FR.lng + NR.lng)/2] - kaho

0
您可以使用 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...

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接