地图上两点之间的MKDirectionsRequest

4
我有以下的代码,总是返回“无法确定路线”(或类似的消息)。
   -(void)getPathDirections:(CLLocationCoordinate2D)source withDestination:(CLLocationCoordinate2D)destination{

MKPlacemark *placemarkSrc = [[MKPlacemark alloc] initWithCoordinate:source addressDictionary:nil];
MKMapItem *mapItemSrc = [[MKMapItem alloc] initWithPlacemark:placemarkSrc];
MKPlacemark *placemarkDest = [[MKPlacemark alloc] initWithCoordinate:destination addressDictionary:nil];
MKMapItem *mapItemDest = [[MKMapItem alloc] initWithPlacemark:placemarkDest];
[mapItemSrc setName:@"name1"];
[mapItemDest setName:@"name2"];

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:mapItemSrc];
[request setDestination:mapItemDest];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
request.requestsAlternateRoutes = NO;

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

[directions calculateDirectionsWithCompletionHandler:
 ^(MKDirectionsResponse *response, NSError *error) {
     if (error) {
         // Handle Error
     } else {
         [_mapView removeOverlays:_mapView.overlays];
         [self showRoute:response];
     }
 }];

}

我在罗马尼亚,我的国家在苹果支持路线的国家列表中。

同时,我能够在当前位置和地图上的标记之间建立路线...

有什么想法吗?


1
在创建您的mapItem时,您是否具有有效的坐标?我认为代码看起来很好。应该是传递给您的方法的坐标有问题。在坐标上放置NSLog并检查它们。 - Pancho
@PanayotPanayotov 是的,传递的坐标没问题。 - Tudor Ion Codârnai
那么响应中没有路线,对吧?给我坐标,我在这里试试。 - Pancho
@PanayotPanayotov 以这个为例 source = CLLocationCoordinate2DMake(24.51898765563965, 44.26686859130859); destination = CLLocationCoordinate2DMake(24.3679256439209, 44.10536575317383); 尽管您可以使用任何坐标... 国家并不重要,都会出现相同的错误。 - Tudor Ion Codârnai
1个回答

3

你的代码在欧洲坐标方面表现完美。你在上面评论中提供的坐标是沙特阿拉伯的,而不是罗马尼亚的。请确保你拥有有效的坐标。最简单的方法是进入谷歌地图,在搜索框中输入你的纬度经度值,然后查看它们在地图上的位置。

P.S 你的错误在于颠倒了纬度经度的值。将它们换个顺序再次检查 :)

应该这样写:

source =  CLLocationCoordinate2DMake(24.51898765563965, 44.26686859130859); 
destination = CLLocationCoordinate2DMake(24.3679256439209, 44.10536575317383);

请使用以下内容:

source =  CLLocationCoordinate2DMake(44.26686859130859, 24.51898765563965); 
destination = CLLocationCoordinate2DMake(44.10536575317383, 24.3679256439209);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接