我在当前用户位置和地图上的某个点之间创建了一条路线。我可以从MKRoute获得有关方向的说明。问题是: 如何检测下一个十字路口何时需要左转或右转?看起来应该是这样的:“在下一个十字路口左转”。 我很乐意在Swift或Objective C中看到解决方案。
答案 0 :(得分:0)
您可以使用以下代码执行此操作
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.
}
}
}];
浏览不同的MKRoute
属性以获得您想要的内容。
答案 1 :(得分:0)
项目,您将看到自定义注释,在mapView上显示源和目标坐标。
时间到两个注释之间的方向。
请点击此链接: - https://developer.apple.com/documentation/mapkit/mkroute
MKRoute 对象定义路径的几何图形,即包含与特定地图坐标关联的线段。路线对象还可以包括其他信息,例如路线的名称,距离和预期的行驶时间。例如,一个步骤可能涉及一条道路,直到需要转弯为止。
在viewController.m类中添加以下代码。
-(void)viewDidAppear:(BOOL)animated
{
MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:sourceCoordinate.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *sourceMapItem = [[MKMapItem alloc]initWithPlacemark:source];
MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:destinationCoordinate.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
[request setSource:sourceMapItem];
[request setDestination:distMapItem];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
MKDirections *direction = [[MKDirections alloc]initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (!error) {
for (MKRoute *route in [response routes]) {
[_mMapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads];
}
}
}];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
[renderer setStrokeColor:[UIColor redColor]];
[renderer setLineWidth:3.0];
return renderer;
}
return nil;
}
对于Swift
更新
override func viewDidLoad() {
super.viewDidLoad()
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude:"", longitude: ""), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: "", longitude: ""), addressDictionary: nil))
request.requestsAlternateRoutes = true
request.transportType = .automobile
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.mapView.add(route.polyline)
self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
return renderer
}