2017-08-31 144 views
-2

我有繞多段線繪製多邊形的問題。 我有每個折線點的座標,我想獲得這條折線周圍的多邊形座標。 我使用MapBox的地圖。 任何想法?我沒有找到解決方案。 東西,看起來像這樣。 我需要獲取圍繞線繪製多邊形的座標。多邊形與半徑的折線?

enter image description here

+1

你是什麼意思**多邊形繞多段線** ?? Sry不能幫你。 –

+0

你的意思是「超越」而不是「縮進」嗎? 「縮進」是向內移動,你聽起來像你想向外移動。你如何處理目標?他們應該是正方形,圓形,有角度嗎?也許還有一張圖來說明你已經或已經嘗試過的一些代碼。 – ColGraff

+0

另外,您使用的是哪些類:[UIBezierPath](https://developer.apple.com/documentation/uikit/uibezierpath),[GMSPolyline](https://developers.google.com/maps/documentation/ios- sdk/reference/interface_g_m_s_polyline),[GMSPolygon](https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_polygon),[MGLPolylineFeature](https://mapbox.github.io/mapbox- GL-天然的/的MacOS/0.5.0 /含量%20Primitives.html#/ C:objc(CS)MGLPolylineFeature)?多邊形的最終用途是什麼? – ColGraff

回答

2

我找到了解決方案,對球員的榜樣!那真是diffucult:d 根據有關草坪:)

我發現莢「SwiftTurf」

var coordsPointer = UnsafeMutablePointer<CLLocationCoordinate2D>.allocate(capacity: Int(polyline.pointCount)) 
    polyline.getCoordinates(coordsPointer, range: NSMakeRange(0, Int(polyline.pointCount))) 

    // save coords 
    var lineCoords: [CLLocationCoordinate2D] = [] 
    for i in 0..<polyline.pointCount { 
     lineCoords.append(coordsPointer[Int(i)]) 
    } 

    let lineString:LineString = LineString(geometry: lineCoords) 

    let bufferLineString = SwiftTurf.buffer(lineString, distance: width, units: .Meters) 

    let outer = bufferLineString!.geometry![0] 
    let interiors = bufferLineString?.geometry![1..<bufferLineString!.geometry.count].map({ coords in 
     return MGLPolygon(coordinates: coords, count: UInt(coords.count)) 
    }) 
    // This polygon is solution 
    self.currentBufferPolygon = MGLPolygon(coordinates: outer, count: UInt(outer.count), interiorPolygons: interiors) 
    mapView.addAnnotation(self.currentBufferPolygon!) 

U可以在github的詳細信息在羣的回購:)祝你好運最後一抹!

0

如果你正在尋找周圍繪製在瀏覽器中折線多邊形,我建議使用turf.js.草皮的buffer method應該適用於這個確切的情況。

這裏有一個Mapbox GL JS地圖

var line = { 
 
    "type": "Feature", 
 
    "properties": {}, 
 
    "geometry": { 
 
    "type": "LineString", 
 
    "coordinates": [ 
 
     [-122.40447521209718, 
 
     37.79367718768535 
 
     ], 
 
     [-122.40803718566895, 
 
     37.79171022624846 
 
     ], 
 
     [-122.40769386291502, 
 
     37.79096412372944 
 
     ], 
 
     [-122.40662097930908, 
 
     37.789641468930114 
 
     ], 
 
     [-122.40941047668457, 
 
     37.789675383451495 
 
     ], 
 
     [-122.40992546081543, 
 
     37.78875968591083 
 
     ], 
 
     [-122.40962505340575, 
 
     37.78791180770003 
 
     ] 
 
    ] 
 
    } 
 
}; 
 

 
mapboxgl.accessToken = 'pk.eyJ1IjoibWFwc2FtIiwiYSI6ImNqNzI4ODR4djBkZmczMnJzZjg3eXZhcDgifQ.5xM2OR_RvuO6YvirBVeiOg'; 
 
var map = new mapboxgl.Map({ 
 
    container: 'map', 
 
    style: 'mapbox://styles/mapbox/light-v9', 
 
    zoom: 15, 
 
    center: [-122.4067, 37.7899] 
 
}); 
 

 
map.on('load', function() { 
 
    map.addLayer({ 
 
    "id": "route", 
 
    "type": "line", 
 
    "source": { 
 
     "type": "geojson", 
 
     "data": line 
 
    } 
 
    }); 
 

 
    var polygon = turf.buffer(line, 50, 'meters'); 
 

 
    map.addLayer({ 
 
    "id": "poly", 
 
    "type": "fill", 
 
    "source": { 
 
     "type": "geojson", 
 
     "data": polygon 
 
    }, 
 
    "layout": {}, 
 
    "paint": { 
 
     "fill-color": '#d9d838', 
 
     "fill-opacity": 0.3 
 
    } 
 
    }); 
 
});
body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
#map { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    width: 100%; 
 
}
<html> 
 

 
<head> 
 
    <meta charset='utf-8' /> 
 
    <title></title> 
 
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> 
 
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.js'></script> 
 
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.css' rel='stylesheet' /> 
 

 
    <script src='https://npmcdn.com/@turf/turf/turf.min.js'></script> 
 
</head> 
 

 
<body> 
 
    <div id='map'></div> 
 
</body> 
 

 
</html>

+0

哇,它看起來像一個解決方案,但我如何適應它的Swift? – Denis