いくつかの異なるラベルと小さな MKMapView を含むビューを持つ iOS アプリがあります。
マップ ビューで、旅行ルートを (ポリラインを使用して) 描画し、ルートの始点と終点に 1 つずつ、2 つの注釈ピンを追加しました。
私がやろうとしているのは、マップのズーム/回転を変更して、ポリラインと 2 つの注釈ピンがマップ ビューのサイズに合うようにすることだけです。しかし、私が試したことはすべて失敗しました。
私のアプローチ
1) ポリラインをマップ ビューに追加します。
2) を使用setVisibleMapRect
して、ポリラインがマップ内に収まるようにマップ ビューを調整します。(edge insets パラメータを使用して、パディングも追加しました)。
3)MKMapCamera
ポリラインとピンが水平方向に表示されるように、マップの回転を変更するために使用します。
これが私のコードです:
// Convert the preview string to a
// MKPolyline map direction object.
MKPolyline *map_data = [self polyline_with_encoded_string:[trip preview]];
// Add the polyline to the map view.
[main_map addOverlay:map_data];
// Set the display area around the polyline.
[main_map setVisibleMapRect:[map_data boundingMapRect] edgePadding:UIEdgeInsetsMake(28.0, 28.0, 28.0, 28.0) animated:YES];
// Calculate the current clockwise degree.
double degree = [self calculate_bearing_angle:[trip.startPoint latitude] :[trip.startPoint longitude] :[trip.stopPoint latitude] :[trip.stopPoint longitude]];
// Change the degree to the approriate
// trip polyline degree (anti-clockwise).
if ((degree >= 0) && (degree <= 90)) {
degree = (270 + degree);
}
else if ((degree > 90) && (degree <= 180)) {
degree = (270 - (degree - 90));
}
else if ((degree > 180) && (degree <= 270)) {
degree = (360 - (degree - 180));
}
else if ((degree > 270) && (degree <= 360)) {
degree = (90 + degree);
}
// Rotate the map so that the trip polyline
// fits horizontally rather than vertically.
MKMapCamera *map_camera = [[main_map camera] copy];
[map_camera setHeading:degree];
[main_map setCamera:map_camera animated:YES];
次の方法を使用して、ポリラインの角度を把握しています。
-(double)calculate_bearing_angle:(double)start_lat :(double)start_lon :(double)end_lat :(double)end_lon {
// Get the seperate latitude/longitude values.
double from_lat = DEGREES_TO_RADIANS(start_lat);
double from_lon = DEGREES_TO_RADIANS(start_lon);
double to_lat = DEGREES_TO_RADIANS(end_lat);
double to_lon = DEGREES_TO_RADIANS(end_lon);
// Calculate the bearing angle.
double degree = RADIANS_TO_DEGREES(atan2(sin(to_lon - from_lon) * cos(to_lat), cos(from_lat) * sin(to_lat) - sin(from_lat) * cos(to_lat) * cos(to_lon - from_lon)));
if (degree >= 0) {
return degree;
}
else {
return (360 + degree);
}
}
私はすべてを試しましたが、最終結果は、特定の角度の特定のポリラインが正しい水平角度に回転し、他のポリラインはそうではありません....私は何を間違っていますか? または、この問題に対するより良い組み込みのアプローチがありますか?
私がやりたいのは、これを変更することだけです:
...これに: