首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mapkit中显示两个位置之间的路径

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

Stack Overflow用户
提问于 2015-05-06 16:21:28
回答 2查看 2.2K关注 0票数 0

我想在mapkit中显示两个位置的距离,我已经尝试了我的过程,它不显示两个位置之间的距离。尝试下面的代码它不工作,谁能帮助我在编码。

代码语言:javascript
复制
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;
}

请帮帮我。

EN

回答 2

Stack Overflow用户

发布于 2015-05-06 16:44:59

如果您在-calculateDirectionsWithCompletionHandler:中选中error

代码语言:javascript
复制
[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);
    }
}];

你可能会看到

Error Domain=MKErrorDomain Code=5 "Directions Not Available“UserInfo=0x7fefe3bb5660 {无法确定到最近道路的NSLocalizedFailureReason=A路线。,MKErrorGEOError=-403,MKDirectionsErrorCode=6,NSLocalizedDescription=Directions不可用}

这意味着不可能从给定位置计算方向。

票数 1
EN

Stack Overflow用户

发布于 2015-05-06 17:24:14

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

代码语言:javascript
复制
- (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];

}

代码语言:javascript
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30071223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档