ios – 在MKMapView上绘制Great Circle叠加线

ios – 在MKMapView上绘制Great Circle叠加线,第1张

概述我正在尝试在MKMapView上的两个纬度/经度点之间绘制一条Great Circle线.这是一条看起来是圆形的线(地球上的“直线”)并且最好可视化 here.事实上,这个非常奇怪的WordPress网站似乎开始准确描述如何做到这一点,但它在最初的几个之后突然结束脚步. 阅读Apple的文档,我看到了 In iOS 4.0 and later, you can also use projected 我正在尝试在MKMapVIEw上的两个纬度/经度点之间绘制一条Great Circle线.这是一条看起来是圆形的线(地球上的“直线”)并且最好可视化 here.事实上,这个非常奇怪的WordPress网站似乎开始准确描述如何做到这一点,但它在最初的几个之后突然结束脚步.

阅读Apple的文档,我看到了

In iOS 4.0 and later,you can also use projected map coordinates instead of regions to specify some values. When you project the curved surface of the globe onto a flat surface,you get a two-dimensional version of a map where longitude lines appear to be parallel. Locations and distances on this map are specifIEd using the MKMapPoint,MKMapSize,and MKMapRect data types. You can use these data types to specify the map’s visible region and when specifying the location of overlays.

我如何将它应用于Great Circle叠加我不确定.有人可以帮忙吗?

解决方法 我已经实现了这个目的,为使用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;}

希望这可以帮助.

Screenshot http://s1-03.twitpicproxy.com/photos/large/489178500.png

总结

以上是内存溢出为你收集整理的ios – 在MKMapView上绘制Great Circle叠加线全部内容,希望文章能够帮你解决ios – 在MKMapView上绘制Great Circle叠加线所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1010278.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存