Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've implemented this for drawing a great circle route for aircraft going between two airports using MKPolyline. </p> <pre><code>+ (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&lt;=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&lt;numsegs; i++) { CLLocationCoordinate2D coord = coords[i]; if(prevCoord.longitude &lt; -170 &amp;&amp; prevCoord.longitude &gt; -180 &amp;&amp; prevCoord.longitude &lt; 0 &amp;&amp; coord.longitude &gt; 170 &amp;&amp; coord.longitude &lt; 180 &amp;&amp; coord.longitude &gt; 0) { twoLines = YES; coords2 = malloc(sizeof(CLLocationCoordinate2D) * (numsegs - i)); numsegs2 = numsegs - i; for(int j=0; j&lt;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]; } } </code></pre> <p>You've now created the overlay(s), now you just need to provide an MKOverlayView in mapView:viewForOverlay.</p> <pre><code>- (MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id&lt;MKOverlay&gt;)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; } </code></pre> <p>Hope this helps.</p> <p><a href="http://s1-03.twitpicproxy.com/photos/large/489178500.png">Screenshot http://s1-03.twitpicproxy.com/photos/large/489178500.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload