2017-12-04 6 views
0

私は、アプリケーションのインスタンス内の1つの注釈とユーザー位置と注釈とユーザーの場所を結ぶルートを示す全体的なポリラインである右のズームですべてのビューに収まる方法を探しています。Mapbox iOS SDKのマップビューに収まるように、ポリラインとユーザーの場所を含むすべての注釈を入力しますか?

私はマップがのMapViewをロードするたびに呼び出してその下にこのコードを持っている:

func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) { 
     if let location = mapView.userLocation?.location?.coordinate, let annotations = mapView.annotations { 
       let coordinate = annotations.first?.coordinate 
       mapView.setVisibleCoordinateBounds(MGLCoordinateBounds(sw: location, ne: coordinate!), edgePadding: UIEdgeInsetsMake(100, 50, 100, 200), animated: true) 
     } 
    } 

これはルートがほぼ直線ですが、ルートはもう少し複雑であればあるときに座標に適していますが助けにならない。あなたはズームレベルが多角形の重心をつかむためにTruf's centroid機能を使用して知っている場合

答えて

0

、このようMGL map.flyToに適用します:

var centroid = turf.centroid(myCoolFeatureGeometry); 

    map.flyTo({ 
     center: centroid.geometry.coordinates, 
     zoom: 19, 
     curve: 1, 
     screenSpeed: 4, 
     easing(t) { 
     return t; 
     } 
    }); 

あなたはまた、ズームレベルが必要な場合、あなたはそれを使用して取得することができますTurf's bbox(またはenvelope)、その出力をMGLに適用するmap.fitbounds

var features = turf.featureCollection([ 
    turf.point([-75.343, 39.984], {"name": "Location A"}), 
    turf.point([-75.833, 39.284], {"name": "Location B"}), 
    turf.point([-75.534, 39.123], {"name": "Location C"}) 
]); 

var enveloped = turf.envelope(features); 

map.fitBounds(enveloped, { 
    padding: {top: 10, bottom:25, left: 15, right: 5} 
}); 
関連する問題