最近项目要求,详情页有一个公司的地址。点击地址那一栏,会跳出来这个弹出框,跳到相应地图页面进行导航,起点就是用户当前位置,终点就是详情页这个地址,高德和百度默认起点是用户当前位置,目的地需要将详情页地址转为经纬度传过去,话不多说,直接上代码~
1.弹出框
UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *AlertAction1 = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self zhuan];
}];
UIAlertAction *AlertAction2 = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self baidu];
}];
UIAlertAction *AlertAction3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertCon addAction:AlertAction1];
[alertCon addAction:AlertAction2];
[alertCon addAction:AlertAction3];
[self presentViewController:alertCon animated:YES completion:nil];
2.跳转到高德地图,需要传目的地的经纬度和名字
//将填写的地址信息拿过来转为经纬度
-(void)zhuan{
//1.获得输入的地址
NSString *address = self.model.address;
if (address.length==0) return;
//2.开始地理编码
//说明:调用下面的方法开始编码,不管编码是成功还是失败都会调用block中的方法
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
//如果有错误信息,或者是数组中获取的地名元素数量为0,那么说明没有找到
if (error || placemarks.count==0) {
// self.detailAddressLabel.text=@"你输入的地址没找到,可能在月球上";
}else // 编码成功,找到了具体的位置信息
{
//打印查看找到的所有的位置信息
/*
61 name:名称
62 locality:城市
63 country:国家
64 postalCode:邮政编码
65 */
for (CLPlacemark *placemark in placemarks) {
NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);
}
//取出获取的地理信息数组中的第一个显示在界面上
CLPlacemark *firstPlacemark=[placemarks firstObject];
//详细地址名称
// self.detailAddressLabel.text=firstPlacemark.name;
//纬度
CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
//经度
CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
NSLog(@"%f %f",latitude,longitude);
coords2 = CLLocationCoordinate2DMake(latitude,longitude);
Xlat = latitude;
Xlong = longitude;
[self gaode];
}
}];
}
-(void)gaode{
//当前的位置 //
// MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
//起点
//MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];
//目的地的位置
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
toLocation.name = self.model.address;
//currentLocation.name = locationName;
NSArray *items = [NSArray arrayWithObjects: toLocation, nil];
NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES }; //打开苹果自身地图应用,并呈现特定的item
[MKMapItem openMapsWithItems:items launchOptions:options];
}
3.跳转百度,需要传目的地的经纬度和名字
-(void)baidu{
BOOL hasBaiduMap = NO;
BOOL hasGaodeMap = NO;
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
hasBaiduMap = YES;
NSLog(@"yesbaidu");
}else
{
NSLog(@"nobaidu");
}
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
hasGaodeMap = YES;
}
//NSString *url2 = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:39.915,116.404|name:我的位置&destination=latlng:39.915,120.202|name:白杨路199弄&mode=driving"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
NSString *url2 = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f|name:我的位置&destination=latlng:%f|name:%@&mode=driving", Xlat, Xlong,self.model.address]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url2]];
}
网友评论