缩放 MKMapView 以适合折线和注释图钉 - 水平方向

Zoom MKMapView to fit polyline and annotation pins - Horizontal orientation

我有一个 iOS 应用程序,其视图包含一些不同的标签和一个小型 MKMapView。

在地图视图上,我绘制了一条旅行路线(使用折线)并添加了两个注释图钉,一个在路线的起点,另一个在路线的终点。

我想做的就是更改地图 zoom/rotation,以便折线和两个注释图钉适合地图视图的大小。但是我尝试的一切都失败了。

我的做法

1) 将折线添加到地图视图。

2) 使用setVisibleMapRect调整地图视图,使折线适合地图。 (我还通过使用边缘插入参数添加了一些填充)。

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);
    }
}

我已经尝试了所有方法,但无论哪种方式,最终结果都是特定角度的某些折线旋转到正确的水平角度,而其他折线则没有......我到底做错了什么?或者是否有更好的内置方法来解决这个问题?

我只想改变这个:

...为此:

我一直在解决你的问题,对我来说这个方法[[self.map camera] setHeading:90.f];按预期工作,所以你的问题可能是在弧度的计算和后面的过程中,但也许你的问题也与相机副本。我直接在地图的相机上使用这种方法,效果很好。希望对您有所帮助