我有一个 iOS 应用程序,其视图包含几个不同的标签和一个小的 MKMapView。
在地图视图上,我绘制了一条旅行路线(使用折线)并添加了两个注释图钉,一个在路线的起点,另一个在路线的终点。
我要做的就是更改地图缩放/旋转,以便折线和两个注释图钉适合地图视图的大小。然而,我尝试过的一切都失败了。
我的方法
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);
}
}
我已经尝试了一切,但无论哪种方式,最终结果都是某些角度的某些折线旋转到正确的水平角度,而其他折线则没有……我做错了什么?还是有更好的内置方法来解决这个问题?
我想做的就是改变这个:
...对此: