2017-08-31 148 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