5.使用自定义地图进行导航 将起点和终点发送给服务器,由服务器返回导航结果

- (void)drection:(CLLocation*) location {

// 使用自定义地图进行导航  将起点和终点发送给服务器,由服务器返回导航结果

// 1、创建导航请求对象

MKDirectionsRequest*request = [[MKDirectionsRequest alloc]init];

// 2、设置起点和终点

request.source= [MKMapItem  mapItemForCurrentLocation];

CLGeocoder*geocoder =[[CLGeocoder alloc]init];

[geocoder geocodeAddressString:@"上海"completionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

if(placemarks.count==0|| error) {

return;

}

CLPlacemark*clPm = placemarks.lastObject;

MKPlacemark*pm = [[MKPlacemark alloc]initWithPlacemark:clPm];

request.destination= [[MKMapItem alloc]initWithPlacemark:pm];

//3.创建导航对象

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

//4.计算导航路线传递数据给服务器

[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse* response,NSError*_Nullableerror) {

for(MKRoute*route in response.routes) {

for(MKRouteStep*step in route.steps) {

NSDictionary*dict = [NSDictionary dictionaryWithObjects:@[step.instructions,@(step.distance)]forKeys:@[@"details",@"distance"]];

NSLog(@"dict = %@",dict);

}

[_mapViewaddOverlay:route.polyline];

}

}];

}];

}

你可能感兴趣的:(5.使用自定义地图进行导航 将起点和终点发送给服务器,由服务器返回导航结果)