如何在mapkit中显示两个位置之间的路径

时间:2023-02-01 16:13:49

I want to show two location's distance in mapkit, I have tried my process its not showing the distance between the location. tried below codes its not working, can any one help me in coding.

我想在mapkit中显示两个位置的距离,我已经尝试了我的过程,它没有显示位置之间的距离。尝试下面的代码不起作用,任何人都可以帮我编码。

MKMapView *   mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 294, 320, 122)];
mapView.showsUserLocation = YES;
mapView.delegate = self;
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
 MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(9.9176458, 78.1228237) addressDictionary:nil];
[request setSource:[[MKMapItem alloc] initWithPlacemark:placemark1]];

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(13.0475604, 80.2089535) addressDictionary:nil];
request.destination = [[MKMapItem alloc] initWithPlacemark:placemark];


// [request setDestination:myMapItem];
    [request setTransportType:MKDirectionsTransportTypeAutomobile]; // This can be limited to automobile and walking directions.
    [request setRequestsAlternateRoutes:YES]; // Gives you several route options.
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (!error) {
            for (MKRoute *route in [response routes]) {
                [mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels.
                // You can also get turn-by-turn steps, distance, advisory notices, ETA, etc by accessing various route properties.
            }
        }
    }];
[self.view addSubview:mapView];



- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    MKPolylineRenderer *renderer =
    [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    renderer.strokeColor = [UIColor blueColor];
    renderer.lineWidth = 5.0;
    return renderer;
}

Please any help me .

请帮帮我。

2 个解决方案

#1


If you check error in the -calculateDirectionsWithCompletionHandler:

如果检查-calculateDirectionsWithCompletionHandler中的错误:

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
    if (!error) {
        for (MKRoute *route in [response routes]) {
            [mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels.
            // You can also get turn-by-turn steps, distance, advisory notices, ETA, etc by accessing various route properties.
        }
    }
    else {
        NSLog(@"%@", error);
    }
}];

You will probably see

你可能会看到

Error Domain=MKErrorDomain Code=5 "Directions Not Available" UserInfo=0x7fefe3bb5660 {NSLocalizedFailureReason=A route to the nearest road cannot be determined., MKErrorGEOError=-403, MKDirectionsErrorCode=6, NSLocalizedDescription=Directions Not Available}

错误域= MKErrorDomain代码= 5“方向不可用”UserInfo = 0x7fefe3bb5660 {NSLocalizedFailureReason =无法确定到最近道路的路线。,MKErrorGEOError = -403,MKDirectionsErrorCode = 6,NSLocalizedDescription =路线不可用}

Which means that it is not possible to calculate directions from given location.

这意味着无法从给定位置计算方向。

#2


Here is my code; I use CLLocationManager Delegate to compute distance between two location and MKMapView Delegate. here is my code, it may help you.

这是我的代码;我使用CLLocationManager Delegate来计算两个位置和MKMapView Delegate之间的距离。这是我的代码,它可能对你有帮助。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)location{
float distance = 0.0f;
NSMutableArray *locationArray = [[NSMutableArray alloc] init];
for (CLLocation *newLocation in location) {
    [locationArray addObject:newLocation];
}

for (int i = 0 ; i < locationArray.count; i++) {
    CLLocation *newLocation = [locationArray objectAtIndex:i];
    distance += [newLocation distanceFromLocation:[locationArray objectAtIndex:i+1]];
}         
NSLog(@"%f",distance);
CLLocationCoordinate2D cordinates [locationsArray.count];
MKPolyline *userLocationsPolyLine = [MKPolyline polylineWithCoordinates:cordinates count:locationsArray.count];
[userMapView setDelegate:self];
[userMapView addOverlay:userLocationsPolyLine];

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{    
    if ([overlay isKindOfClass:[MKPolyline class]]){
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;
        return renderer;
    }
    return nil;
}

#1


If you check error in the -calculateDirectionsWithCompletionHandler:

如果检查-calculateDirectionsWithCompletionHandler中的错误:

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
    if (!error) {
        for (MKRoute *route in [response routes]) {
            [mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels.
            // You can also get turn-by-turn steps, distance, advisory notices, ETA, etc by accessing various route properties.
        }
    }
    else {
        NSLog(@"%@", error);
    }
}];

You will probably see

你可能会看到

Error Domain=MKErrorDomain Code=5 "Directions Not Available" UserInfo=0x7fefe3bb5660 {NSLocalizedFailureReason=A route to the nearest road cannot be determined., MKErrorGEOError=-403, MKDirectionsErrorCode=6, NSLocalizedDescription=Directions Not Available}

错误域= MKErrorDomain代码= 5“方向不可用”UserInfo = 0x7fefe3bb5660 {NSLocalizedFailureReason =无法确定到最近道路的路线。,MKErrorGEOError = -403,MKDirectionsErrorCode = 6,NSLocalizedDescription =路线不可用}

Which means that it is not possible to calculate directions from given location.

这意味着无法从给定位置计算方向。

#2


Here is my code; I use CLLocationManager Delegate to compute distance between two location and MKMapView Delegate. here is my code, it may help you.

这是我的代码;我使用CLLocationManager Delegate来计算两个位置和MKMapView Delegate之间的距离。这是我的代码,它可能对你有帮助。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)location{
float distance = 0.0f;
NSMutableArray *locationArray = [[NSMutableArray alloc] init];
for (CLLocation *newLocation in location) {
    [locationArray addObject:newLocation];
}

for (int i = 0 ; i < locationArray.count; i++) {
    CLLocation *newLocation = [locationArray objectAtIndex:i];
    distance += [newLocation distanceFromLocation:[locationArray objectAtIndex:i+1]];
}         
NSLog(@"%f",distance);
CLLocationCoordinate2D cordinates [locationsArray.count];
MKPolyline *userLocationsPolyLine = [MKPolyline polylineWithCoordinates:cordinates count:locationsArray.count];
[userMapView setDelegate:self];
[userMapView addOverlay:userLocationsPolyLine];

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{    
    if ([overlay isKindOfClass:[MKPolyline class]]){
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;
        return renderer;
    }
    return nil;
}