項目中用到點擊店家地址跳到地圖導航,app本身沒有其他地圖相關(guān)功能,所以沒有必要去集成那些sdk,文件大還很麻煩。直接調(diào)用多簡單方便,還不用占用自己app。最可貴的是蘋果還有自帶地圖呢
在調(diào)用地圖之前,先啰嗦一下,由于項目中只返回了店家地址,沒有經(jīng)緯度,所以這里,我要先記錄一下,用蘋果自帶方法,將地址轉(zhuǎn)換成經(jīng)緯度
1、引入頭文件 #import<CoreLocation/CoreLocation.h>
2、聲明屬性
{
? ? CLGeocoder*_geocoder;
}
@property(nonatomic,assign)CLLocationCoordinate2D? coordinate;
3、在你需要調(diào)用的地方
_geocoder= [[CLGeocoderalloc]init];
[self getCoordinateByAddress:_address];
#pragma mark根據(jù)地名確定地理坐標
-(void)getCoordinateByAddress:(NSString *)address{
//地理編碼
? [_geocodergeocodeAddressString:addresscompletionHandler:^(NSArray*placemarks,NSError*error) {
//取得第一個地標,地標中存儲了詳細的地址信息,注意:一個地名可能搜索出多個地址
?? ? CLPlacemark*placemark=[placemarksfirstObject];
?? ? CLLocation*location=placemark.location;//位置
?? ? CLRegion*region=placemark.region;//區(qū)域
?? ? NSDictionary*addressDic= placemark.addressDictionary;//詳細地址信息字典,包含以下部分信息
//? ? ? ? NSString *name=placemark.name;//地名
//? ? ? ? NSString *thoroughfare=placemark.thoroughfare;//街道
//? ? ? ? NSString *subThoroughfare=placemark.subThoroughfare; //街道相關(guān)信息,例如門牌等
//? ? ? ? NSString *locality=placemark.locality; // 城市
//? ? ? ? NSString *subLocality=placemark.subLocality; // 城市相關(guān)信息,例如標志性建筑
//? ? ? ? NSString *administrativeArea=placemark.administrativeArea; // 州
//? ? ? ? NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政區(qū)域信息
//? ? ? ? NSString *postalCode=placemark.postalCode; //郵編
//? ? ? ? NSString *ISOcountryCode=placemark.ISOcountryCode; //國家編碼
//? ? ? ? NSString *country=placemark.country; //國家
//? ? ? ? NSString *inlandWater=placemark.inlandWater; //水源、湖泊
//? ? ? ? NSString *ocean=placemark.ocean; // 海洋
//? ? ? ? NSArray *areasOfInterest=placemark.areasOfInterest; //關(guān)聯(lián)的或利益相關(guān)的地標
?? ? NSLog(@"位置:%@,區(qū)域:%@,詳細信息:%@",location,region,addressDic);
?? ? CLLocationDegreeslatitude = location.coordinate.latitude;
?? ? CLLocationDegreeslongitude = location.coordinate.longitude;
?? ? self.coordinate = CLLocationCoordinate2DMake(latitude,longitude);
?NSLog(@"緯度-->%lf,經(jīng)度-->%lf",latitude,longitude);
?? //傳給接口的緯度和經(jīng)度
//? ? ? self->_latitude = [NSString stringWithFormat:@"%lf",latitude];
//? ? ? self->_longitude = [NSString stringWithFormat:@"%lf",longitude];
?? }];
}
以上就是先把地址轉(zhuǎn)換成經(jīng)緯度的過程。
接下來,就是將經(jīng)緯度傳給蘋果,高德,百度地圖進行跳轉(zhuǎn)導航的過程啦~
1、調(diào)用蘋果自帶地圖,記得引用頭文件#import<MapKit/MapKit.h>
2、Info.plist文件中加入白名單
高德:iosamap
百度:baidumap

3、代碼展示
#pragma mark-- 根據(jù)經(jīng)緯度跳地圖導航
-(void)tapAddressAction{
? ??__weaktypeof(self) weakSelf =self;
? ? UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"選擇地圖" preferredStyle:UIAlertControllerStyleActionSheet];
? ? UIAlertAction*action1 = [UIAlertActionactionWithTitle:@"蘋果自帶地圖"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
? ? ? ? MKMapItem*currentLocation = [MKMapItemmapItemForCurrentLocation];
? ? ? ? MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:weakSelf.coordinate addressDictionary:nil]];
? ? ? ? tolocation.name= weakSelf.address;
? ? ? ? [MKMapItem openMapsWithItems:@[currentLocation,tolocation]launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
? ? ? ? MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
? ? }];
? ? if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
? ? ? ? UIAlertAction*action2 = [UIAlertActionactionWithTitle:@"高德地圖"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
? ? ? ? ? ? NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",weakSelf.coordinate.latitude,weakSelf.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
? ? ? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
? ? ? ? }];
? ? ? ? [alertaddAction:action2];
? ? }
? ? if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
? ? ? ? UIAlertAction*action3 = [UIAlertActionactionWithTitle:@"百度地圖"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
? ? ? ? ? ? NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",weakSelf.coordinate.latitude,weakSelf.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
? ? ? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
? ? ? ? }];
? ? ? ? [alertaddAction:action3];
? ? }
? ? UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
? ? }];
? ? [alertaddAction:action1];
? ? [alertaddAction:action4];
? ? [self.navigationController presentViewController:alert animated:YES completion:nil];
}
我這里會先判斷是否安裝了高德和百度地圖,如果安裝了,會在選擇彈窗中顯示,沒安裝就不顯示,只顯示一個蘋果自帶地圖