有没有办法在mkmapview中使用内置的Apple API获取路线? [英] is there a way to get directions in mkmapview using a built in apple API?
问题描述
我知道谷歌地图是最好的地图,但我不想下载一堆额外的库和所有这些。我更喜欢做一些快速而简单的事情来获得从A点到B点的快速路线并完成它。有没有办法用内置的函数/库来做到这一点?有人能指出我正确的方向吗?
I know google maps are known to be the best maps out there, but i dont want to have to download a bunch of extra libraries and all that. I'd prefer to do something quick and simple to get a quick route from point A to B and be done with it. Is there a way to do this with built in functions/libraries? Can somebody point me in the right direction?
编辑
我是在我的情况下,我没有试图转弯方向或任何东西,我只是想从头到尾划一条线。也许可以选择要走哪条路线。有没有办法做到或没有?
I'm not trying to get turn by turn directions or anything in my case, i just want to draw a line from start to finish. maybe give options about which routes to take. Is there a way to do it or no?
推荐答案
在iOS 7中,您可以使用<$ c $获取并显示路线c> MKDirectionsRequest 。
In iOS 7, you can get and display directions using MKDirectionsRequest
.
以下是一些示例代码,用于显示从当前位置到另一个地图项目的路线:
Here's some sample code for displaying directions from the current location to another map item:
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:[MKMapItem mapItemForCurrentLocation]];
[request setDestination:myMapItem];
[request setTransportType:MKDirectionsTransportTypeAny]; // 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]) {
[myMapView 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.
}
}
}];
如果你是iOS 7新手,你需要实现 mapView:rendererForOverlay:
显示任何叠加层的方法。类似于:
If you're new to iOS 7, you'll need to implement the mapView:rendererForOverlay:
method for any overlay to appear. Something like:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
[renderer setStrokeColor:[UIColor blueColor]];
[renderer setLineWidth:5.0];
return renderer;
}
return nil;
}
这篇关于有没有办法在mkmapview中使用内置的Apple API获取路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!