33

ユーザーが iOS アプリを通じて記録した CLLocations のプログラムで作成された配列を保持する配列 allCollections があります。allCollections の各サブ配列には、旅行のすべての地点が保持されます。

MKMapView でこれらの旅行を表すために、allCollections の配列の CLLocations から MKPolylines を描画します。私の質問は次のとおりです。ポリラインがマップに追加された場合、プログラムでマップをズームおよびセンタリングしてすべてを表示するにはどうすればよいでしょうか?

4

8 に答える 8

71
-(void)zoomToPolyLine: (MKMapView*)map polyline: (MKPolyline*)polyline animated: (BOOL)animated
{
    [map setVisibleMapRect:[polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) animated:animated];
}
于 2013-09-27T03:18:38.953 に答える
14
-(void)zoomToPolyLine: (MKMapView*)map polyLine: (MKPolyline*)polyLine 
animated (BOOL)animated
{
MKPolygon* polygon = 
    [MKPolygon polygonWithPoints:polyLine.points count:polyLine.pointCount];

[map setRegion:MKCoordinateRegionForMapRect([polygon boundingMapRect]) 
     animated:animated];
}
于 2013-02-04T21:51:39.190 に答える
14

迅速に:

if let first = mapView.overlays.first {
    let rect = mapView.overlays.reduce(first.boundingMapRect, combine: {MKMapRectUnion($0, $1.boundingMapRect)})
    mapView.setVisibleMapRect(rect, edgePadding: UIEdgeInsets(top: 50.0, left: 50.0, bottom: 50.0, right: 50.0), animated: true)
}

これにより、小さなバッファーですべてのオーバーレイに合わせてズーム/パンします

于 2015-12-07T18:17:46.877 に答える
10

CLLocations座標を記録するすべてをループし、それを使用して、この質問iOS MKMapView zoom to show all markermax/minで行うようにビュー rect を設定できます。

または、各オーバーレイを調べて、それらboundingMapRectを使用することもできますMKMapRectUnion( http://developer.apple.com/library/ios/documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html#//apple_ref/c/func /MKMapRectUnion ) をMKMapRect使用して、それらすべてをカバーするものになるまでそれらをすべて結合し、それを使用してビューを設定します。

[mapView setVisibleMapRect:zoomRect animated:YES]

この質問は、私が提案したように、maprects をユニオンで結合する単純なループを示しています

于 2012-11-26T20:45:44.667 に答える
0

@Fundtimer はそれを正しい方法で指摘しました。唯一のことは、視覚的なニーズに応じてパディングを調整する必要があるということです。そのようにして、他のすべてのオーバーレイをサポートし、以下はすべてのオーバーレイの一般的なソリューションです。

-(void)zoomIntoExistingMapObjectForAnnot:(CustomMapAnnotation *)annot
{
   id overlay = annot.shape;//I have overlay property in custom annotation class.
   [_mapView setVisibleMapRect:[overlay boundingMapRect] edgePadding:UIEdgeInsetsMake(150.0, 150.0, 150.0, 150.0) animated:YES];
}
于 2017-07-25T06:54:43.333 に答える