How can i add polygon on google maps IOS?

3.8k Views Asked by At

I want to add polygon on google maps view but I have this error

no visible @ interface GMSMapView declares the selector add overlay

Code:

CLLocationCoordinate2D commuterLotCoords[5]={
        CLLocationCoordinate2DMake(39.048019,-76.850535),
        CLLocationCoordinate2DMake(39.048027,-76.850234),
        CLLocationCoordinate2DMake(39.047407,-76.850181),
        CLLocationCoordinate2DMake(39.047407,-76.8505),
        CLLocationCoordinate2DMake(39.048019,-76.850535)
    };

MKPolygon *commuterPoly1 = [MKPolygon polygonWithCoordinates:commuterLotCoords count:5];
[mapView addOverlay:commuterPoly1];
4

There are 4 best solutions below

0
On
GMSPolygon * polygon = [[GMSPolygon alloc] init];
GMSMutablePath * rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(39.048019,-76.850535)];
[rect addCoordinate:CLLocationCoordinate2DMake(39.048027,-76.850234)];
[rect addCoordinate:CLLocationCoordinate2DMake(39.047407,-76.850181)];
[rect addCoordinate:CLLocationCoordinate2DMake(39.047407,-76.8505)];
[rect addCoordinate:CLLocationCoordinate2DMake(39.048019,-76.850535)];
polygon.path=rect;
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.2f];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = mapView;
0
On

Slight change to Dheeraj's Answer : aka Updated for Swift 4

func tempPolygons(){
    let polygon = GMSPolygon()
    let rect = GMSMutablePath()
    rect.add(CLLocationCoordinate2DMake(18.546160, 73.903992))
    rect.add(CLLocationCoordinate2DMake(18.546099, 73.904817))
    rect.add(CLLocationCoordinate2DMake(18.545922, 73.905643))
    rect.add(CLLocationCoordinate2DMake(18.545854, 73.905973))
    rect.add(CLLocationCoordinate2DMake(18.545105, 73.905915))
    rect.add(CLLocationCoordinate2DMake(18.543153, 73.904867))
    rect.add(CLLocationCoordinate2DMake(18.543016, 73.903805))
    rect.add(CLLocationCoordinate2DMake(18.545473, 73.903863))
    rect.add(CLLocationCoordinate2DMake(18.546140, 73.904013))

    polygon.path = rect
    polygon.fillColor = #colorLiteral(red: 0.9474907517, green: 0.2350950539, blue: 0.1785519123, alpha: 0.24)
    //polygon.strokeColor = UIColor.black
    polygon.strokeWidth = 0
    polygon.map = mapView
}
0
On

Swift 2.2:

 let polygon = GMSPolygon()
        let rect = GMSMutablePath()
        rect.addCoordinate(CLLocationCoordinate2DMake(22.7196, 75.8577))
        rect.addCoordinate(CLLocationCoordinate2DMake(22.7396, 75.8577))
        rect.addCoordinate(CLLocationCoordinate2DMake(22.7196, 75.1577))
        rect.addCoordinate(CLLocationCoordinate2DMake(22.9196, 76.8577))
        rect.addCoordinate(CLLocationCoordinate2DMake(22.9196, 75.8577))
        rect.addCoordinate(CLLocationCoordinate2DMake(23.7196, 77.8577))

        polygon.path = rect
        polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.2)
        polygon.strokeColor = UIColor.blackColor()
        polygon.strokeWidth = 2
        polygon.map = mapView
0
On

To draw polygon on GoogleMap you should use GMSPolygon class. Steps to draw polygon:

  1. Create GMSPolygon
  2. Create GMSPath using co-ordinates.
  3. Customize the GMSPolygon (color, width..)
  4. Assign map view to the Polygon.Map property

To draw on Apple Map

  1. Use MKPolygon
  2. Load the co-ordinates
  3. Call Addoverlay/Overlays using map view and give the paths
  4. customize the rendererforoverlay

Same steps works for Polyline.