如何找到当前位置和存储位置之间的道路方向?

时间:2014-12-07 18:40:26

标签: ios objective-c annotations mapkit plist

我正在使用mapkit和CLLCOATION框架在Objective c上构建我的应用程序。我想获取当前位置和固定位置之间的方向STORED IN .plist但它不能与我合作

我收到此错误错误错误域= MKErrorDomain代码= 5“方向不可用”{NSLocalizedFailureReason =无法确定从最近的道路到目的地的路线,MKErrorGEOError = -403,MKDirectionsErrorCode = 7,NSLocalizedDescription =路线不可用处理方向。注意我在欧洲支持的苹果位置使用了位置。

我使用了以下代码

- (void) getDirection {
    MKDirectionsRequest *request =[[MKDirectionsRequest alloc]init];
    request.source= [MKMapItem mapItemForCurrentLocation];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    CLLocationDegrees longitude = [defaults doubleForKey:@"longitude"];
    CLLocationDegrees latitude = [defaults doubleForKey:@"latitude"];


    MKPlacemark *destinationPlaceMark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude,longitude) addressDictionary:nil];

    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlaceMark];
    request.destination =destination;
    request.transportType=MKDirectionsTransportTypeAny;
    request.requestsAlternateRoutes = NO;
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {
         if (error) {
             NSLog(@"error %@ with handling diretion", error);
         } else {
             [self showRoute:response];

         }
     }];
}

- (void)showRoute:(MKDirectionsResponse *)response

{
    for (MKRoute *route in response.routes)
    { NSLog(@"new route");

        [self.map addOverlay:route.polyline level:MKOverlayLevelAboveRoads];

        for(MKRouteStep *step in route.steps)
        {
            NSLog(@"%@",step.instructions);
            [self.directionarray addObject:step.instructions];
        }
        NSLog(@"saving direction...");
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:self.directionarray forKey:@"Direction"];
        [defaults synchronize];

    }

}

0 个答案:

没有答案