如何在iOS Swift中使用Bing Maps绘制无相交多边形?
如何在Bing Maps(Swift)中绘制无自相交的多边形?
我需要实现类似某款Google Maps SDK解决方案的功能——绘制无自相交的多边形,但要适配Bing Maps SDK。目前我的代码会根据添加的点位绘制多边形,但无法处理自相交的情况,恳请帮忙解决。
以下是我当前的实现代码:
var positions: [MSGeoposition] = [] var pinLayer: MSMapElementLayer! var mapView: MSMapView! var pinImage: UIImage? var untitledPushpinCount = 0 func addPin(atLocation location: MSGeopoint, withTitle title: String) { let pushpin = MSMapIcon() pushpin.location = location pushpin.title = title if self.pinImage != nil { pushpin.image = self.pinImage pushpin.normalizedAnchorPoint = CGPoint(x: 0.5, y: 1.0) } // 添加点位并触发多边形绘制 let position = MSGeoposition(latitude: location.position.latitude, longitude: location.position.longitude) positions.append(position) highlightArea(position: positions) pushpin.accessibilityLabel = "Pushpin" if title.isEmpty { untitledPushpinCount += 1 pushpin.accessibilityValue = String(format: "Untitled %d", untitledPushpinCount) } else { pushpin.accessibilityValue = title } self.pinLayer.elements.add(pushpin) } // 绘制多边形的方法 func highlightArea(position: [MSGeoposition]) { let geopath = MSGeopath(positions: position) // 创建多边形元素 let mapPolygon = MSMapPolygon() mapPolygon.paths = [geopath] mapPolygon.zIndex = 1 mapPolygon.fillColor = .clear mapPolygon.strokeColor = .blue mapPolygon.strokeWidth = 1 mapPolygon.strokeDashed = true // 添加多边形到地图图层 let highlightsLayer = MSMapElementLayer() highlightsLayer.zIndex = 1 highlightsLayer.elements.add(mapPolygon) mapView.layers.add(highlightsLayer) }
内容的提问来源于stack exchange,提问作者Dhananjay Kumar
相关产品推荐
相关产品推荐

