【问题标题】:Adding hole in polygon with google maps for ios使用 ios 的谷歌地图在多边形中添加孔
【发布时间】:2015-02-23 10:41:29
【问题描述】:

我想在我的应用程序中使整个地图变暗,除了一个正常显示的小“选定”部分。

为了在 Google Maps API for Android 中实现该功能,我创建了一个多边形并像这样调用 setHoles 函数:

List points = Arrays.asList(new LatLng(-90 + delta, -180 + delta),
                new LatLng(-90 + delta, 0),
                new LatLng(-90 + delta, 180 - delta),
                new LatLng(0, 180 - delta),
                new LatLng(90 - delta, 180 - delta),
                new LatLng(90 - delta, 0),
                new LatLng(90 - delta, -180 + delta),
                new LatLng(0, -180 + delta));
defaultZone = map.addPolygon(new PolygonOptions().addAll(points));
defaultZone.setFillColor(0x99000000);

defaultZone.setHoles(Arrays.asList(pointsLatLong));

这对 Android 非常有用,我想在 iOS 应用程序中重现相同的效果,但 GMSPolygon 类没有 setHoles 函数,并且我不知道如何在我的多边形中创建孔(我尝试在地图上绘制深色多边形,然后在其上方绘制带有透明颜色的“选定”区域,但它不起作用)。

有人可以帮助我吗?这似乎很奇怪,一个功能可以在 Android 中完成,但不能在 iOS 中使用相同的框架

【问题讨论】:

  • 看看这是否解决了你的问题link
  • @larryp 我看到了这个问题,但它对我不起作用。我试图改变其中一个多边形的方向,但它给了我奇怪的多边形。我认为这可能是因为我创建了只有 1 条路径的多边形,但是没有办法使用 iOS 创建具有 2 条路径的多边形 :(

标签: ios google-maps google-maps-sdk-ios


【解决方案1】:

迅速

override func loadView() {
  let hydeParkLocation = CLLocationCoordinate2D(latitude: -33.87344, longitude: 151.21135)
  let camera = GMSCameraPosition.camera(withTarget: hydeParkLocation, zoom: 16)
  let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
  mapView.animate(to: camera)

  let hydePark = "tpwmEkd|y[QVe@Pk@BsHe@mGc@iNaAKMaBIYIq@qAMo@Eo@@[Fe@DoALu@HUb@c@XUZS^ELGxOhAd@@ZB`@J^BhFRlBN\\BZ@`AFrATAJAR?rAE\\C~BIpD"
  let archibaldFountain = "tlvmEqq|y[NNCXSJQOB[TI"
  let reflectionPool = "bewmEwk|y[Dm@zAPEj@{AO"

  let polygon = GMSPolygon()
  polygon.path = GMSPath(fromEncodedPath: hydePark)
  polygon.holes = [GMSPath(fromEncodedPath: archibaldFountain)!, GMSPath(fromEncodedPath: reflectionPool)!]
  polygon.fillColor = UIColor(colorLiteralRed: 1.0, green: 0.0, blue: 0.0, alpha: 0.2)
  polygon.strokeColor = UIColor(colorLiteralRed: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
  polygon.strokeWidth = 2
  polygon.map = mapView
  view = mapView
}

目标-c

- (void)loadView {
  CLLocationCoordinate2D hydeParkLocation = CLLocationCoordinate2DMake(-33.87344, 151.21135);
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:hydeParkLocation
                                                             zoom:16];
  _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

  NSString *hydePark = @"tpwmEkd|y[QVe@Pk@BsHe@mGc@iNaAKMaBIYIq@qAMo@Eo@@[Fe@DoALu@HUb@c@XUZS^ELGxOhAd@@ZB`@J^BhFRlBN\\BZ@`AFrATAJAR?rAE\\C~BIpD";
  NSString *archibaldFountain = @"tlvmEqq|y[NNCXSJQOB[TI";
  NSString *reflectionPool = @"bewmEwk|y[Dm@zAPEj@{AO";

  GMSPolygon *polygon = [[GMSPolygon alloc] init];
  polygon.path = [GMSPath pathFromEncodedPath:hydePark];
  polygon.holes = @[[GMSPath pathFromEncodedPath:archibaldFountain],
                    [GMSPath pathFromEncodedPath:reflectionPool]];
  polygon.fillColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.2];
  polygon.strokeColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
  polygon.strokeWidth = 2;
  polygon.map = _mapView;
  self.view = _mapView;
}

reference

【讨论】:

  • 我遵循谷歌文档中的这段代码,它工作正常,但是当我只更改位置坐标时,它不会在该位置显示多边形......!或突出显示该位置...!您能解释一下如何绘制或突出显示该位置吗?有什么我想念的吗?
【解决方案2】:

这是 iOS SDK 的一个已知限制。正在此处跟踪请求:

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

去投票吧!

【讨论】:

  • 如果只有 1 个孔,则有部分解决方法。看起来您可以在路径中包含 1 个孔并且它会正确渲染。这显然不是解决方案,因此需要在 SDK 中添加适当的支持。
  • 我只需要 1 个孔,但我做不到。路径应该是什么样子?
【解决方案3】:

这是一个已知请求,Google 很可能会在下一个 iOS 地图 SDK 版本中包含该请求。

在此链接上评论https://code.google.com/p/gmaps-api-issues/issues/detail?id=5464

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2012-09-12
    • 1970-01-01
    • 2012-07-01
    相关资源
    最近更新 更多