2017-11-23 10 views
1

私は緯度と経度の2つの配列を持っており、これら2つのアレイの値からポリラインを描く必要があります。緯度と経度のマップボックスを使用してポリラインを描く配列 - スイフト3 iOS

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

私はその作業を確認しました – Myaaoonn

答えて

1

あなたはポリライン注釈を作成したい場合は、このコードでそれを行うことができます:あなたが希望の場合

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) 
} 
+0

ありがとうございます! –

-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 
} 

その完璧な

+0

実際にポリゴンではなくポリゴンを2つの異なる配列、つまりlatitudeArrayとlongitudeから描画する必要があります。 –

+0

これは近いです! 'MGLPolyline'の' MGLPolygon'を交換して、それに応じてスタイリングメソッドを変更するだけです。 – jmkiley

関連する問題