如何在谷歌地图IOS上添加多边形?

时间:2015-06-09 07:45:58

标签: ios google-maps polygon

我想在谷歌地图视图上添加多边形,但我有这个错误

no visible @ interface GMSMapView declares the selector add overlay

代码:

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 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

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

答案 2 :(得分:1)

对Dheeraj的回答略有更改:aka已更新为 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
}

答案 3 :(得分:0)

要在GoogleMap上绘制多边形,您应该使用GMSPolygon类。 绘制多边形的步骤:

  1. 创建GMSPolygon
  2. 使用坐标创建GMSPath。
  3. 自定义GMSPolygon(颜色,宽度..)
  4. 将地图视图指定给Polygon.Map属性
  5. 在Apple Map上绘图

    1. 使用MKPolygon
    2. 加载坐标
    3. 使用地图视图调用Addoverlay / Overlays并提供路径
    4. 自定义rendererforoverlay
    5. 相同的步骤适用于Polyline。