You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:42:21