我试图在MKMapView上的两个lat/lon点之间画一条伟大的圆圈线。这是一条显示为圆形(地球仪上的“直线”)的线条,并且是最好的可视化这里。事实上,这个非常奇怪的WordPress站点似乎开始准确地描述如何做到这一点,但在最初的几步之后,它突然结束了。
阅读苹果的文档我看到了
在iOS 4.0及更高版本中,您还可以使用投影地图坐标而不是区域来指定某些值。当你把地球的曲面投影到一个平面上时,你会得到一张二维的地图,其中的经线看起来是平行的。此地图上的位置和距离是使用MKMapPoint、MKMapSize和MKMapRect数据类型指定的。您可以使用这些数据类型来指定映射的可见区域和指定覆盖的位置。
我不确定如何将这个应用到一个大圆圈覆盖中。有人能帮忙吗?
发布于 2012-01-04 23:45:32
我已经实现了这一点,为飞机在两个机场之间使用MKPolyline绘制一个很大的圆周路线。
+ (void)createGreatCircleMKPolylineFromPoint:(CLLocationCoordinate2D)point1
toPoint:(CLLocationCoordinate2D)point2
forMapView:(MKMapView*)mapView
{
double lat1 = point1.latitude;
double lon1 = point1.longitude;
double lat2 = point2.latitude;
double lon2 = point2.longitude;
lat1 = lat1 * (PI/180);
lon1 = lon1 * (PI/180);
lat2 = lat2 * (PI/180);
lon2 = lon2 * (PI/180);
double d = 2 * asin( sqrt(pow(( sin( (lat1-lat2)/2) ), 2) + cos(lat1) * cos(lat2) * pow(( sin( (lon1-lon2)/2) ), 2)));
int numsegs = 100;
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numsegs);
double f = 0.0;
for(int i=1; i<=numsegs; i++)
{
f += 1.0 / (float)numsegs;
double A=sin((1-f)*d)/sin(d);
double B=sin(f*d)/sin(d);
double x = A*cos(lat1) * cos(lon1) + B * cos(lat2) * cos(lon2);
double y = A*cos(lat1) * sin(lon1) + B * cos(lat2) * sin(lon2);
double z = A*sin(lat1) + B*sin(lat2);
double latr=atan2(z, sqrt(pow(x, 2) + pow(y, 2) ));
double lonr=atan2(y, x);
double lat = latr * (180/PI);
double lon = lonr * (180/PI);
// NSLog(@"lat: %f lon: %f", lat, lon);
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(lat, lon);
coords[i - 1] = loc;
}
//check for circling west to east. If the plane is crossing 180, we need
//to draw two lines or else the polyline connects the dots and draws a straight
//line all the way across the map.
CLLocationCoordinate2D prevCoord;
BOOL twoLines = NO;
int numsegs2 = 0;
CLLocationCoordinate2D *coords2;
for(int i=0; i<numsegs; i++)
{
CLLocationCoordinate2D coord = coords[i];
if(prevCoord.longitude < -170 && prevCoord.longitude > -180 && prevCoord.longitude < 0
&& coord.longitude > 170 && coord.longitude < 180 && coord.longitude > 0)
{
twoLines = YES;
coords2 = malloc(sizeof(CLLocationCoordinate2D) * (numsegs - i));
numsegs2 = numsegs - i;
for(int j=0; j<numsegs2; j++)
{
coords2[j] = coords[i + j];
}
break;
}
prevCoord = coord;
}
//remove any previously added overlays
[mapView removeOverlays:mapView.overlays];
if(twoLines)
{
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:numsegs - numsegs2];
free(coords);
[mapView addOverlay:polyline];
MKPolyline *polyline2 = [MKPolyline polylineWithCoordinates:coords2 count:numsegs2];
free(coords2);
[mapView addOverlay:polyline2];
}
else
{
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:numsegs];
free(coords);
[mapView addOverlay:polyline];
}
}
现在您已经创建了覆盖,现在只需要在mapView:viewForOverlay中提供一个MKOverlayView。
- (MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKPolyline *polyline = (MKPolyline*)overlay;
MKPolylineView *view = [[[MKPolylineView alloc] initWithPolyline:polyline] autorelease];
//choose your line params here
view.lineWidth = 2;
view.fillColor = [UIColor blueColor];
return view;
}
希望这能有所帮助。
截图http://s1-03.twitpicproxy.com/photos/large/489178500.png
发布于 2017-05-19 16:19:02
这是游戏的后期,但值得一提的是MKGeodesicPolyline
,新的自iOS 7.0,它“跟踪最短的路径沿地球表面”。
这样,创建和添加一个MKPolylineOverlay
类型的测地线就变得简单了。
points = [CLLocationCoordinate2DMake(27.123, 85.765),
CLLocationCoordinate2DMake(41.444, 106.987)]
geodesic = MKGeodesicPolyline(coordinates: points, count: 2)
mapView.add(geodesic)
请记住包括呈现器,并给mapView一个委托:
//MARK: - MKMapView Delegate Method
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKGeodesicPolyline {
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.white
polylineRenderer.lineWidth = 2.5
return polylineRenderer
}
}
发布于 2011-07-13 09:23:45
这可以完成创建MKOverlayPathView类的子类。您需要覆盖(void)createPath方法,基本上可以使用UIBezierPath创建弧,或者直接创建弧形作为路径,这是可能的,但我还没有完成。
一旦在方法上定义了路径,就需要使用新创建的路径设置类的path属性。这样,路径的呈现就会自动完成。
希望这能有所帮助。
https://stackoverflow.com/questions/6104517
复制相似问题