Google Map半径缩放级别 [英] Google Map radius to zoom level

查看:83
本文介绍了Google Map半径缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a map with a custom UIView on top of it. I'm calculating the radius of the circle of according to the zoom level.

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)
}

现在,我想根据给定的半径缩放地图吗?我该怎么办?

Now, I want to zoom the map according to a given radius? How could I do that ?

推荐答案

在圆的周长上拾取任意坐标,然后将地图的缩放比例或相机的缩放比例设置为该坐标的边界,从而增加一定的填充量.现在,按照这种逻辑,如果用户启用了地图缩放功能,则每次地图的摄像机移动或重新放置时,都必须重新计算一次.

Pick any coordinate on the perimeter of the circle, and set the map's zoom or camera's zoom to the bounds of that coordinate, adding some padding ofcourse. Now for this logic, if your map is user enabled to be zoomed, you again have to recalculate everytime your map's camera is moveid or recentered.

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

You can use this method to do so : mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))

此处的边界是 GMSCoordinateBounds 对象.

希望这会有所帮助

这篇关于Google Map半径缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆