本文介绍了无法绘制MKPolylineView横经度+/- 180的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MKMapView上绘制MKPolylineView时遇到问题。这条线代表了环球旅行,它始于纽约附近,始终向东行进。这次旅行的一站,从日本到旧金山,穿越太平洋,因此经度+/- 180。 MKPolylineView确实连接了这两个点,但它的行进方向错误。也就是说,这条线路从日本西部返回旧金山,而不是向东穿过太平洋。我没有看到任何选项让我指定一个线段应该连接两个点的方向。

I'm having a problem drawing an MKPolylineView on an MKMapView. The line represents a trip around the world, which begins and ends near New York, always traveling east. One leg of the trip, from Japan to San Francisco, crosses the Pacific ocean, and therefore longitude +/-180. The MKPolylineView does connect those two points, but it travels in the wrong direction. That is, the line travels west from Japan back to San Francisco, instead of east across the Pacific. I don't see any option that lets me specify which direction a line segment should travel to connect two points.

简单地说,MKMapView似乎没有明白世界是圆的。有没有办法可以按照预定的方式划线,从日本东行到旧金山?

To put it simply, the MKMapView doesn't seem to understand that the world is round. Is there a way I can draw the line as intended, traveling east from Japan to San Francisco?

我有一个截图,可以清楚地显示问题:

Polyline http://www.builtlight.org/pub/MKPolylineView.png

I have a screenshot that displays the problem clearly:
The Polyline http://www.builtlight.org/pub/MKPolylineView.png

推荐答案

这似乎是对MKMapView的限制。

This seems to be a limitation of the MKMapView.

解决方法是将+/- 180交叉分成东和西部分。

A workaround is to split the +/-180 crossing into "east" and "west" parts.

对于环球旅行(完成==开始) ),你可以把交叉点的西部分放在折线的前面。

例如:

For an around-the-world trip (finish == start), you can put the "west" part of the crossing at the front of the polyline.
For example:

CLLocationCoordinate2D newYorkCoord = CLLocationCoordinate2DMake(40.7141667, -74.0063889);
CLLocationCoordinate2D londonCoord = CLLocationCoordinate2DMake(51.5, -0.116667);
CLLocationCoordinate2D japanCoord = CLLocationCoordinate2DMake(36, 138);
CLLocationCoordinate2D sanFranciscoCoord = CLLocationCoordinate2DMake(37.775, -122.4183333);

CLLocationCoordinate2D japanToSFMidpointEast;
//use average calc as crude way to find midpoint...
japanToSFMidpointEast.latitude = (japanCoord.latitude + sanFranciscoCoord.latitude)/2.0;
japanToSFMidpointEast.longitude = 180;

CLLocationCoordinate2D japanToSFMidpointWest;
japanToSFMidpointWest.latitude = japanToSFMidpointEast.latitude;
japanToSFMidpointWest.longitude = -180;

int pointsCount = 6;
CLLocationCoordinate2D *points = malloc(pointsCount * sizeof(CLLocationCoordinate2D));
points[0] = japanToSFMidpointWest;
points[1] = sanFranciscoCoord;
points[2] = newYorkCoord;
points[3] = londonCoord;
points[4] = japanCoord;
points[5] = japanToSFMidpointEast;
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:points count:pointsCount];
[mapView addOverlay:polyline];
free(points);
points = NULL;



如果旅行不在世界各地(完成!=开始),你将不得不使用两条折线。

这个例子从日本到SF:


If a trip is not around-the-world (finish != start), you'll have to use two polylines.
This example goes from Japan to SF:

CLLocationCoordinate2D japanCoord = CLLocationCoordinate2DMake(36, 138);
CLLocationCoordinate2D sanFranciscoCoord = CLLocationCoordinate2DMake(37.775, -122.4183333);

CLLocationCoordinate2D japanToSFMidpointEast;
japanToSFMidpointEast.latitude = (japanCoord.latitude + sanFranciscoCoord.latitude)/2.0;
japanToSFMidpointEast.longitude = 180;

CLLocationCoordinate2D japanToSFMidpointWest;
japanToSFMidpointWest.latitude = japanToSFMidpointEast.latitude;
japanToSFMidpointWest.longitude = -180;

int eastPointsCount = 2;
CLLocationCoordinate2D *eastPoints = malloc(eastPointsCount * sizeof(CLLocationCoordinate2D));
eastPoints[0] = japanCoord;
eastPoints[1] = japanToSFMidpointEast;
MKPolyline *eastPolyline = [MKPolyline polylineWithCoordinates:eastPoints count:eastPointsCount];
[mapView addOverlay:eastPolyline];
free(eastPoints);
eastPoints = NULL;

int westPointsCount = 2;
CLLocationCoordinate2D *westPoints = malloc(westPointsCount * sizeof(CLLocationCoordinate2D));
westPoints[0] = japanToSFMidpointWest;
westPoints[1] = sanFranciscoCoord;
MKPolyline *westPolyline = [MKPolyline polylineWithCoordinates:westPoints count:westPointsCount];
[mapView addOverlay:westPolyline];
free(westPoints);
westPoints = NULL;

这篇关于无法绘制MKPolylineView横经度+/- 180的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:40