Google Maps SDK iOS - 根据缩放级别计算半径

时间:2015-08-11 13:54:56

标签: ios google-maps google-maps-api-3 zoom

我需要计算半径以根据相机缩放级别在地图上显示标记。现在我已经获得了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

    }

4 个答案:

答案 0 :(得分:15)

好的,我的问题找到了很好的答案。也许它对任何人都有帮助。根据这个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)
    }

答案 1 :(得分:4)

将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()将返回半径

答案 2 :(得分:0)

而不是southWest,您应该使用near/far + Left/Right。请参阅GMSVisibleRegion Struct Reference。原因是southWest没有涵盖3D地图的情况。

获得farRight,farLeft,nearRight和nearLeft之后,您可以计算两个水平边缘的两个中点之间的距离,以及垂直边缘的距离。然后你选择较小的值,这应该是你的半径。

答案 3 :(得分: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...

}