Unable to draw MKPolylineView crossing longitude +/-180

前端 未结 1 883
北恋
北恋 2021-01-20 00:13

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. O

相关标签:
1条回答
  • 2021-01-20 00:50

    This seems to be a limitation of the MKMapView.

    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;
    


    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;
    
    0 讨论(0)
提交回复
热议问题