我有两个纬度和经度数组,我需要从这两个数组的值中绘制一条折线。我可以使用以下代码从预设值中绘制多边形:
var points = [
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562),
CLLocationCoordinate2D(latitude: 40.718249486603604, longitude: -74.00725364685059),
CLLocationCoordinate2D(latitude: 40.720656417464404, longitude: -74.00545120239258),
CLLocationCoordinate2D(latitude: 40.718379593199494, longitude: -74.00519371032715),
CLLocationCoordinate2D(latitude: 40.717273679029205, longitude: -74.00639533996582),
CLLocationCoordinate2D(latitude: 40.713435363794270, longitude: -73.99841308593750),
CLLocationCoordinate2D(latitude: 40.714150998671556, longitude: -73.99755477905273),
CLLocationCoordinate2D(latitude: 40.716037635568070, longitude: -73.99643898010254),
CLLocationCoordinate2D(latitude: 40.728201906826750, longitude: -73.99137496948242),
CLLocationCoordinate2D(latitude: 40.743810548166270, longitude: -73.97961616516113),
CLLocationCoordinate2D(latitude: 40.746671735171680, longitude: -73.98613929748535),
CLLocationCoordinate2D(latitude: 40.735941649217736, longitude: -73.99377822875977),
CLLocationCoordinate2D(latitude: 40.738673108048920, longitude: -73.99970054626465),
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562)
]
let polygon = MGLPolygon(coordinates: &points, count: UInt(points.count))
map.add(polygon)
但无法绘制折线。
答案 0 :(得分:0)
如果您想创建折线注释,可以使用以下代码执行此操作:
var points = [
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562),
CLLocationCoordinate2D(latitude: 40.718249486603604, longitude: -74.00725364685059),
CLLocationCoordinate2D(latitude: 40.720656417464404, longitude: -74.00545120239258),
CLLocationCoordinate2D(latitude: 40.718379593199494, longitude: -74.00519371032715),
CLLocationCoordinate2D(latitude: 40.717273679029205, longitude: -74.00639533996582),
CLLocationCoordinate2D(latitude: 40.713435363794270, longitude: -73.99841308593750),
CLLocationCoordinate2D(latitude: 40.714150998671556, longitude: -73.99755477905273),
CLLocationCoordinate2D(latitude: 40.716037635568070, longitude: -73.99643898010254),
CLLocationCoordinate2D(latitude: 40.728201906826750, longitude: -73.99137496948242),
CLLocationCoordinate2D(latitude: 40.743810548166270, longitude: -73.97961616516113),
CLLocationCoordinate2D(latitude: 40.746671735171680, longitude: -73.98613929748535),
CLLocationCoordinate2D(latitude: 40.735941649217736, longitude: -73.99377822875977),
CLLocationCoordinate2D(latitude: 40.738673108048920, longitude: -73.99970054626465),
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562)
]
let line = MGLPolyline(coordinates: &points, count: UInt(points.count))
mapView.addAnnotation(line)
如果您想创建折线样式图层,可以使用以下代码执行此操作:
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
var points = [
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562),
CLLocationCoordinate2D(latitude: 40.718249486603604, longitude: -74.00725364685059),
CLLocationCoordinate2D(latitude: 40.720656417464404, longitude: -74.00545120239258),
CLLocationCoordinate2D(latitude: 40.718379593199494, longitude: -74.00519371032715),
CLLocationCoordinate2D(latitude: 40.717273679029205, longitude: -74.00639533996582),
CLLocationCoordinate2D(latitude: 40.713435363794270, longitude: -73.99841308593750),
CLLocationCoordinate2D(latitude: 40.714150998671556, longitude: -73.99755477905273),
CLLocationCoordinate2D(latitude: 40.716037635568070, longitude: -73.99643898010254),
CLLocationCoordinate2D(latitude: 40.728201906826750, longitude: -73.99137496948242),
CLLocationCoordinate2D(latitude: 40.743810548166270, longitude: -73.97961616516113),
CLLocationCoordinate2D(latitude: 40.746671735171680, longitude: -73.98613929748535),
CLLocationCoordinate2D(latitude: 40.735941649217736, longitude: -73.99377822875977),
CLLocationCoordinate2D(latitude: 40.738673108048920, longitude: -73.99970054626465),
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562)
]
let line = MGLPolylineFeature(coordinates: &points, count: UInt(points.count))
let source = MGLShapeSource(identifier: "line", shape: line, options: nil)
style.addSource(source)
let layer = MGLLineStyleLayer(identifier: "line-layer", source: source)
style.addLayer(layer)
}
答案 1 :(得分:-1)
使用以下代码检查
mapVw.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapVw.setCenter(CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562), zoomLevel: 11, animated: false)
mapVw.delegate = self
var coordinates = [
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562),
CLLocationCoordinate2D(latitude: 40.718249486603604, longitude: -74.00725364685059),
CLLocationCoordinate2D(latitude: 40.720656417464404, longitude: -74.00545120239258),
CLLocationCoordinate2D(latitude: 40.718379593199494, longitude: -74.00519371032715),
CLLocationCoordinate2D(latitude: 40.717273679029205, longitude: -74.00639533996582),
CLLocationCoordinate2D(latitude: 40.713435363794270, longitude: -73.99841308593750),
CLLocationCoordinate2D(latitude: 40.714150998671556, longitude: -73.99755477905273),
CLLocationCoordinate2D(latitude: 40.716037635568070, longitude: -73.99643898010254),
CLLocationCoordinate2D(latitude: 40.728201906826750, longitude: -73.99137496948242),
CLLocationCoordinate2D(latitude: 40.743810548166270, longitude: -73.97961616516113),
CLLocationCoordinate2D(latitude: 40.746671735171680, longitude: -73.98613929748535),
CLLocationCoordinate2D(latitude: 40.735941649217736, longitude: -73.99377822875977),
CLLocationCoordinate2D(latitude: 40.738673108048920, longitude: -73.99970054626465),
CLLocationCoordinate2D(latitude: 40.729437724412420, longitude: -74.00527954101562)
]
let shape = MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count))
mapVw.add(shape)
func mapView(_ mapView: MGLMapView, alphaForShapeAnnotation annotation: MGLShape) -> CGFloat {
return 0.5
}
func mapView(_ mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {
return .red
}
func mapView(_ mapView: MGLMapView, fillColorForPolygonAnnotation annotation: MGLPolygon) -> UIColor {
return .yellow
}
完美