@SanMao
2015-08-05T16:28:04.000000Z
字数 1952
阅读 1452
定位/地图
MKMapItem
// 获取地点的MKMapItemMKMapItem *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和终点的MKMapItemMKPlacemark *mkpm = [[MKPlacemark alloc] initWithPlacemark:pm];// 获取终点的MKMapItemMKMapItem *destilationItem = [[MKMapItem alloc] initWithPlacemark:mkpm];
openMapsWithItems:launchOptions
// 包装起始点和目的地的itemNSArray *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;}