如何使用 MKpolyline 对象从 Apple 地图获取有效坐标

How to get valid coodinates from Apple map using MKpolyline object

我正在尝试获取多段线的坐标,以便我可以在路径中放置一些所需的标记,但我得到的坐标不是有效坐标。

-(void)getPathCoordinates
{
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

MKPlacemark *sourcePlaceMark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(37.775246, -122.41954)];
MKMapItem *sourceMapItem = [[MKMapItem alloc] initWithPlacemark: sourcePlaceMark];

MKPlacemark *destinationPlaceMark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(37.775141, -122.42279)];
MKMapItem *destinationMapItem = [[MKMapItem alloc] initWithPlacemark: destinationPlaceMark];

[request setSource:sourceMapItem];
[request setDestination:destinationMapItem];
request.transportType = MKDirectionsTransportTypeAutomobile;

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
    if (error != nil)
    {
        DDLogDebug(@"%@", [error localizedDescription]);
        return;
    }
    
    if (response != nil) {
        MKRoute* route = response.routes.firstObject;
        
        self.routeLine = route.polyline;
        [self.mapView addOverlay:self.routeLine level:MKOverlayLevelAboveRoads];
        
        MKMapRect rect = [route.polyline boundingMapRect];
        [self.mapView setRegion:MKCoordinateRegionForMapRect(rect) animated:YES];
        
        NSUInteger pointCount = route.polyline.pointCount;
        
        for (int c=0; c < (int)pointCount; c++)
        {
            DDLogDebug(@"routeCoordinates = %f, %f",
                 route.polyline.points[c].x, route.polyline.points[c].y);
        }
    }
}];
}

输出为:-

    2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42935228.149851, 103755574.386599

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42935252.234477, 103755716.740228

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42935128.083078, 103755840.320677

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42934520.747859, 103756462.276056

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42934437.756564, 103756466.804150

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42933349.250790, 103757506.748632

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42933295.190872, 103757520.521377

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42933128.089800, 103757555.424898

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42932829.753617, 103757601.554120

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42932491.748639, 103757675.228743

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42931980.229964, 103757749.752291

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42931946.600967, 103757544.293507

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42931836.542430, 103756866.029954

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42931695.240988, 103755986.730208

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42931736.773919, 103755978.145637

2022-01-28 13:18:23 [DEBUG FRBLocationsViewController:273] routeCoordinates = 42932699.786117, 103755820.981766

如您所见,输入坐标是 37.775246, -122.41954 但输出坐标是 42932699.786117, 103755820.981766

有什么方法可以得到相同类型的坐标

route.polyline.points 是地图点... 如果你想要坐标..你必须将MapPoints“转换”为Cooridnates..

for (int c = 0; c < (int)pointCount; c++)
{
     // MapPoints
     NSLog(@"routeMapPoints = %f, %f",
                    route.polyline.points[c].x, route.polyline.points[c].y);
               
      // Coordinates
      CLLocationCoordinate2D coordinate = MKCoordinateForMapPoint(route.polyline.points[c]);
      NSLog(@"routeCoordinates = %f, %f",
                     coordinate.latitude, coordinate.longitude);
}