2017-03-10 6 views
1

私はいくつかの異なるラベルと小さなMKMapViewを含むビューを持つiOSアプリを持っています。ズームMKMapViewがポリラインと注釈ピンにフィット - 水平方向

マップビューでは、(ポリラインを使用して)トリップルートを描画し、2つのアノテーションピンを追加しました.1つはルートの最初に、もう1つはルートの最後に追加されています。

マップズーム/ローテーションを変更して、ポリラインと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); 
    } 
} 

私はすべてを試してみましたが、いずれかの方法は、最終的な結果があります特定の角度で特定のポリラインが正しい水平角度に回転されていないこと、他のものが...私が間違っていることは何ですか?あるいは、この問題に対するより良いアプローチがありますか?これに...

enter image description here

enter image description here

+0

DEGREES_TO_RADIANSに緯度を渡すのはなぜですか? –

答えて

0

を私はあなたの問題に取り組んでいた、私の本のために

私がやりたいすべての

は、これを変更することですメソッド [[self.map camera] setHeading:90.f];は意図したとおりに動作しますので、問題はラジアンと後者のプロセスの計算にありますが、問題はカメラのコピーにも関連しています。私は地図のカメラで直接このメソッドを使用し、素晴らしい作品です。これがあなたに役立つことを願っています

+0

私はそのコードが動作していることを知っています、私の問題は、マップも回転させるべき適切な程度を再計算することです。 – Supertecnoboff

+1

Ahh ok @Supertecnoboffさらに調査するつもりです –

関連する問題