項(xiàng)目截圖
最近項(xiàng)目要求苔埋,詳情頁有一個(gè)公司的地址贱迟。點(diǎn)擊地址那一欄塞关,會(huì)跳出來這個(gè)彈出框,跳到相應(yīng)地圖頁面進(jìn)行導(dǎo)航抬探,起點(diǎn)就是用戶當(dāng)前位置,終點(diǎn)就是詳情頁這個(gè)地址帆赢,高德和百度默認(rèn)起點(diǎn)是用戶當(dāng)前位置小压,目的地需要將詳情頁地址轉(zhuǎn)為經(jīng)緯度傳過去,話不多說匿醒,直接上代碼~
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.跳轉(zhuǎn)到高德地圖场航,需要傳目的地的經(jīng)緯度和名字
//將填寫的地址信息拿過來轉(zhuǎn)為經(jīng)緯度
-(void)zhuan{
//1.獲得輸入的地址
NSString *address = self.model.address;
if (address.length==0) return;
//2.開始地理編碼
//說明:調(diào)用下面的方法開始編碼,不管編碼是成功還是失敗都會(huì)調(diào)用block中的方法
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
//如果有錯(cuò)誤信息廉羔,或者是數(shù)組中獲取的地名元素?cái)?shù)量為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);
}
//取出獲取的地理信息數(shù)組中的第一個(gè)顯示在界面上
CLPlacemark *firstPlacemark=[placemarks firstObject];
//詳細(xì)地址名稱
// self.detailAddressLabel.text=firstPlacemark.name;
//緯度
CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
//經(jīng)度
CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
NSLog(@"%f %f",latitude,longitude);
coords2 = CLLocationCoordinate2DMake(latitude,longitude);
Xlat = latitude;
Xlong = longitude;
[self gaode];
}
}];
}
-(void)gaode{
//當(dāng)前的位置 //
// MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
//起點(diǎn)
//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 }; //打開蘋果自身地圖應(yīng)用孩饼,并呈現(xiàn)特定的item
[MKMapItem openMapsWithItems:items launchOptions:options];
}
3.跳轉(zhuǎn)百度,需要傳目的地的經(jīng)緯度和名字
-(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]];
}