MKDirectionsRequest返回错误的坐标

时间:2015-04-28 09:19:53

标签: ios routes coordinates mapbox directions

我试图在地图上显示点到点之间的路线。我正在使用Mapbox,所以请告诉我,我是否采用了错误的方式,但是我使用MKDirectionsRequest来获取所有需要使用Mapbox类RMPolylineAnnotation显示路径的MKMapPoint。但是,MkDirectionsRequest返回不可用的坐标:

   <__NSArrayM 0x7fd88bfa6810>(<+43224681.35381404,+104172185.41666411>
   +/- 0.00m (speed -1.00 mps / course    -1.00) @ 4/28/15, 10:31:51 AM Central European Summer Time, ...)

当我在调用Directions API之前打印MKDirectionsRequest.source对象时(坐标是正确的):

<MKMapItem: 0x7fca6ad48de0> {
    isCurrentLocation = 0;
    name = "Unknown Location";
    placemark = "<+37.33233141,-122.03121860> +/- 0.00m, region CLCircularRegion (identifier:'<+37.33233141,-122.03121860> radius 0.00', center:<+37.33233141,-122.03121860>, radius:0.00m)";
}

以下是代码:

MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
        MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:_userCoordinates
                                                             addressDictionary:nil];
        MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:carCoordinates
                                                                  addressDictionary:nil];
        directionsRequest.source = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
        directionsRequest.destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
        directionsRequest.transportType = MKDirectionsTransportTypeWalking;
        directionsRequest.requestsAlternateRoutes = NO;

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

        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
             if (error) {
                 // Handle Error
             } else {
                 NSMutableArray *routePoints = [[NSMutableArray alloc] init];
                 MKRoute *route = response.routes[0];

                 for (NSUInteger i = 0; i < route.polyline.pointCount; i++) {
                      MKMapPoint point = route.polyline.points[i];
                      CLLocation *location = [[CLLocation alloc] initWithLatitude:point.x longitude:point.y];
                      [routePoints addObject:location];
                 }

                 RMPolylineAnnotation *polylineAnnotation = [[RMPolylineAnnotation alloc] initWithMapView:_mapView
                                                                                                   points:[routePoints copy]];
                 [polylineAnnotation setLineWidth:10.0f];
                 [polylineAnnotation setLineColor:[UIColor orangeColor]];

                 [_mapView addAnnotation:polylineAnnotation];
             }
         }];

非常感谢你的帮助:)。

0 个答案:

没有答案