How can i add polygon on google maps IOS?

二次信任 提交于 2019-12-12 02:57:17

问题


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];

回答1:


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;



回答2:


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



回答3:


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
}



回答4:


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.



来源:https://stackoverflow.com/questions/30725946/how-can-i-add-polygon-on-google-maps-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!