美文网首页
iOS 原生地图,路线规划

iOS 原生地图,路线规划

作者: 赵永洪 | 来源:发表于2017-03-07 23:43 被阅读263次

虽然是转载的,还是说几句吧。网上百度地图导航路线规划倒是挺多的,苹果自带的高德导航确实挺少,研究了好久发现就这个讲的稍微全一点,把需要用到的类什么的都讲清楚了。不过高德有个方法可以跳转到它自己的地图上,给个起点终点自动帮你计算路线,还有文字标注,这点倒是很方便。

[MKMapItemopenMapsWithItems:itemslaunchOptions:dictM];//这个方法就是跳转到自带地图的,不用手机上安装有高德地图也可以跳转。还是把具体代码贴出来,网上其它地方有,这里就当总结一下。

/**

*开始导航

*/

- (void)startNavigation{

//1,获取用户输入的起点终点

//2,利用GEO对象进行地理编码获取地标对象

CLLocationCoordinate2Dcoords =CLLocationCoordinate2DMake([InforCentershareInfoCenter].lantitude, [InforCentershareInfoCenter].longitude);

CLLocation* currentLocation = [[CLLocationalloc]initWithLatitude:coords.latitudelongitude:coords.longitude];

//2.1获取开始位置的地标

[self.geocoderreverseGeocodeLocation:currentLocationcompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

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

return;

}

//开始位置的地标

CLPlacemark* startPlacemark = [placemarksfirstObject];

//3,获得结束位置的地标

floatlatitude = [[NSStringstringWithFormat:@"%@",self.businessDic[@"Y"]]floatValue];

floatlongitude = [[NSStringstringWithFormat:@"%@",self.businessDic[@"X"]]floatValue];

CLLocationCoordinate2Dcoords1 =CLLocationCoordinate2DMake(latitude,longitude);

CLLocation* currentLocation1 = [[CLLocationalloc]initWithLatitude:coords1.latitudelongitude:coords1.longitude];

[self.geocoderreverseGeocodeLocation:currentLocation1completionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

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

return;

}

CLPlacemark* endPlacemark = [placemarksfirstObject];

//4,获得地标后开始导航

[selfstartNavigationWithStartPlacemark:startPlacemarkendPlacemark:endPlacemark];

}];

}];

}

/**

*利用地标位置开始设置导航

*

*  @param startPlacemark开始位置的地标

*  @param endPlacemark结束位置的地标

*/

-(void)startNavigationWithStartPlacemark:(CLPlacemark*)startPlacemark endPlacemark:(CLPlacemark*)endPlacemark

{

//0,创建起点

MKPlacemark* startMKPlacemark = [[MKPlacemarkalloc]initWithPlacemark:startPlacemark];

//0,创建终点

MKPlacemark* endMKPlacemark = [[MKPlacemarkalloc]initWithPlacemark:endPlacemark];

//1,设置起点位置

MKMapItem* startItem = [[MKMapItemalloc]initWithPlacemark:startMKPlacemark];

//2,设置终点位置

MKMapItem* endItem = [[MKMapItemalloc]initWithPlacemark:endMKPlacemark];

//3,起点,终点数组

NSArray* items = @[ startItem ,endItem];

//4,设置地图的附加参数,是个字典

NSMutableDictionary* dictM = [NSMutableDictionarydictionary];

//导航模式(驾车,步行)

dictM[MKLaunchOptionsDirectionsModeKey] =MKLaunchOptionsDirectionsModeDriving;

//地图显示的模式

dictM[MKLaunchOptionsMapTypeKey] =MKMapTypeStandard;

//只要调用MKMapItem的open方法,就可以调用系统自带地图的导航

//Items:告诉系统地图从哪到哪

//launchOptions:启动地图APP参数(导航的模式/是否需要先交通状况/地图的模式/..)

[MKMapItemopenMapsWithItems:itemslaunchOptions:dictM];

}

//这里是自己计算路线,相当于把起点和终点用线连接,选择不同的模式就会有不同的路线。

- (IBAction)goSearch {

CLLocationCoordinate2DfromCoordinate =_coordinate;

CLLocationCoordinate2DtoCoordinate   =CLLocationCoordinate2DMake(32.010241,

118.719635);

MKPlacemark*fromPlacemark = [[MKPlacemarkalloc]initWithCoordinate:fromCoordinate

addressDictionary:nil];

MKPlacemark*toPlacemark   = [[MKPlacemarkalloc]initWithCoordinate:toCoordinate

addressDictionary:nil];

MKMapItem*fromItem = [[MKMapItemalloc]initWithPlacemark:fromPlacemark];

MKMapItem*toItem   = [[MKMapItemalloc]initWithPlacemark:toPlacemark];

[selffindDirectionsFrom:fromItem

to:toItem];

}

#pragma mark - Private

- (void)findDirectionsFrom:(MKMapItem*)source

to:(MKMapItem*)destination

{

MKDirectionsRequest*request = [[MKDirectionsRequestalloc]init];

request.source= source;

request.destination= destination;

request.requestsAlternateRoutes=YES;

MKDirections*directions = [[MKDirectionsalloc]initWithRequest:request];

[directionscalculateDirectionsWithCompletionHandler:

^(MKDirectionsResponse*response,NSError*error) {

if(error) {

NSLog(@"error:%@", error);

}

else{

MKRoute*route = response.routes[0];

[self.mapViewaddOverlay:route.polyline];

}

}];

}

}

相关文章

网友评论

      本文标题:iOS 原生地图,路线规划

      本文链接:https://www.haomeiwen.com/subject/thbvgttx.html