How to display a circle in GMSMapView

匆匆过客 提交于 2019-11-28 23:29:22

At the moment the SDK doesn't support circles, but there is a feature request to add circles here:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=4971

In the meantime you could maybe fake a circle by drawing a polyline, with several short segments?

It's a bit late since the question is over a year old, but Google searches led me here, so I thought I'd update this. Posterity 4TW!

There is now a GMSCircle which can do, as far as I know, just about everything an MKCircle can.
Google's documentation on the GMSCircle.

// Build a circle for the GMSMapView
GMSCircle *geoFenceCircle = [[GMSCircle alloc] init];
geoFenceCircle.radius = 130; // Meters
geoFenceCircle.position = SOME_CLLOCATION.coordinate; // Some CLLocationCoordinate2D position
geoFenceCircle.fillColor = [UIColor colorWithWhite:0.7 alpha:0.5];
geoFenceCircle.strokeWidth = 3;
geoFenceCircle.strokeColor = [UIColor orangeColor];
geoFenceCircle.map = mapView; // Add it to the map.

It behaves very similarly to an MKCircle (overlay) in that it resizes with the zoom level of the map, etc. Please disregard the blue circle in the center; that's the user location shown on the map view, and I just used the same coordinate for the center point of the GMSCircle.

Super easy. Check out the images:

One zoom level:

And here, we're zoomed out a bit:

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    // Make sure cicle is a GMSCircle Object  and it is a class variable
    circle.map = nil

    let point = mapView.center
    circle.position = mapView.projection.coordinate(for: point)
    circle.radius = 130.0
    circle.fillColor = .clear
    circle.strokeColor = .black
    circle.strokeWidth = 3.4
    circle.map = mapView
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!