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

Ant*_*ton 7 google-maps zoom google-maps-api-3 ios

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

    }
Run Code Online (Sandbox Code Playgroud)

Ant*_*ton 17

好的,我的问题找到了很好的答案.也许它对任何人都有帮助.根据这篇文章

要获得半径应使用下一个示例(所有函数都转换为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)
    }
Run Code Online (Sandbox Code Playgroud)


All*_*ome 6

将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)
    }
}
Run Code Online (Sandbox Code Playgroud)

调用self.map.getRadius()将返回半径