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