@SanMao
2015-08-06T00:28:04.000000Z
字数 1952
阅读 1258
定位/地图
MKMapItem
// 获取地点的MKMapItem
MKMapItem *startItem = [MKMapItem mapItemForCurrentLocation];
MKMapItem
// 拿到用户的输入
NSString *address = self.textField.text;
// 进行地理编码
CLGeocoder *geocoorder = [[CLGeocoder alloc] init];
[geocoorder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == 0 || error) return ;
// 假设取出搜索后的数组中的第一个元素
CLPlacemark *pm = [placemarks firstObject];
// 导航需要起点MKMapItem和终点的MKMapItem
MKPlacemark *mkpm = [[MKPlacemark alloc] initWithPlacemark:pm];
// 获取终点的MKMapItem
MKMapItem *destilationItem = [[MKMapItem alloc] initWithPlacemark:mkpm];
openMapsWithItems:launchOptions
// 包装起始点和目的地的item
NSArray *items = @[sourceItem,destilationitem];
// 包装options,(地图类型,前往方式,交通状况)
NSDictionary *dict =@{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey : @YES
};
// 打开系统地图开始导航
[MKMapItem openMapsWithItems:items launchOptions:dict];
NSLocationAlwaysUsageDescription
)MKDirectionsRequest
// 创建线路请求对象
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
// 获取起点
request.source = sourceItem;
// 获取终点
request.destination = destilationItem;
MKDirections
// 创建线路对象
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
calculateDirectionsWithCompletionHandler:
)并画到mapView上(addOverlay:
)
// 计算所有线路
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (response.routes.count == 0 || error) return ;
// 遍历
for (MKRoute *route in response.routes) {
[self.mapView addOverlay:route.polyline];
}
}];
mapView:rendererForOverlay:
#pragma mark - 渲染线段
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(MKPolyline *) overlay{
// 创建线段的渲染
MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
// 设置线宽和颜色
render.strokeColor = [UIColor blueColor];
render.lineWidth = 5;
return render;
}