简体   繁体   English

如何使用 MKpolyline object 从 Apple map 获取有效坐标

[英]How to get valid coodinates from Apple map using MKpolyline object

I'm trying to get the coordinates of of the polyline so that I can put some desired markers in the path, but the coordinates I'm getting is not the valid one.我正在尝试获取折线的坐标,以便可以在路径中放置一些所需的标记,但我得到的坐标不是有效的。

-(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);
        }
    }
}];
}

Output is:- Output 是:-

    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

As you can see the input coordinates are 37.775246, -122.41954 but the output coordinates are 42932699.786117, 103755820.981766如您所见,输入坐标为37.775246、-122.41954但 output 坐标为42932699.786117、103755820.981766

is there any way I can get the same kind of coordinates有什么办法可以得到相同的坐标

The route.polyline.points are map points... If you wants the coordinates.. you have to "convert" MapPoints to Cooridnates.. route.polyline.points是 map 点...如果您想要坐标..您必须将 MapPoints“转换”为坐标..

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM