How to display a circle in GMSMapView

后端 未结 3 2277
深忆病人
深忆病人 2020-12-15 08:16

I need to display a circle (as with MKCircle) on a GMSMapView. This is easy when using a MKMapView and MKCircle, but can\'t use MKCircle with GMSMapView. Any ideas?

相关标签:
3条回答
  • 2020-12-15 08:54
    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
    }
    
    0 讨论(0)
  • 2020-12-15 08:56

    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?

    0 讨论(0)
  • 2020-12-15 09:01

    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.
    

    //Updating code for Swift 5.3

     let circle = GMSCircle(position: position, radius:10)
     circle.fillColor = .clear
     circle.strokeWidth = 3
     circle.strokeColor = .black
     circle.map = mapView
    

    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: enter image description here

    And here, we're zoomed out a bit: enter image description here

    0 讨论(0)
提交回复
热议问题