在開發(fā)中典鸡,有時候我們并不需要太過于復(fù)雜的地圖功能被廓,或許只需要調(diào)用系統(tǒng)地圖、高德萝玷、百度等主流地圖就可以完成我們所需要的功能伊者。比如調(diào)用三方地圖進行一個線路的導(dǎo)航、或者進行地點的標注间护。那么下面將會為大家介紹如何不集成高德、百度等地圖的SDK挖诸,達到在項目中調(diào)用其的效果汁尺。
調(diào)用系統(tǒng)地圖
調(diào)用iPhone系統(tǒng)地圖的話,我們需要導(dǎo)入一個框架多律,即:MapKit.framework
- 地點標注
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(self.lat.floatValue, self.lon.floatValue) addressDictionary:nil]]; //目的地坐標
toLocation.name = self.destination; //目的地名字
[toLocation openInMapsWithLaunchOptions:nil];
- 線路導(dǎo)航
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(self.lat.floatValue, self.lon.floatValue) addressDictionary:nil]]; //目的地坐標
toLocation.name = self.destination; //目的地名字
[MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:NO]}];
關(guān)于上面這個字典的參數(shù)痴突,給出如下注釋:
/**
MKLaunchOptionsDirectionsModeKey 路線模式,常量
MKLaunchOptionsDirectionsModeDriving 駕車模式
MKLaunchOptionsDirectionsModeWalking 步行模式
MKLaunchOptionsMapTypeKey 地圖類型狼荞,
枚舉 MKMapTypeStandard :標準模式
MKMapTypeSatellite :衛(wèi)星模式
MKMapTypeHybrid :混合模式
MKLaunchOptionsMapCenterKey 中心點坐標辽装, CLLocationCoordinate2D類型
MKLaunchOptionsMapSpanKey 地圖顯示跨度,MKCoordinateSpan 類型
MKLaunchOptionsShowsTrafficKey 是否 顯示交通狀況相味,布爾型
MKLaunchOptionsCameraKey 3D地圖效果拾积,MKMapCamera類型(注意:此屬性從iOS7及以后可用,前面的屬性從iOS6開始可用)
*/
效果圖:
調(diào)用三方地圖的準備工作
當然除了系統(tǒng)地圖丰涉,三方地圖的開發(fā)文檔上也都給的有說明拓巧,這里不詳細解釋對應(yīng)的參數(shù),下面代碼中只挑一些必要的進行解釋一死。
-
配置info.plist
還有就是三方地圖不像系統(tǒng)地圖調(diào)起來這么方便肛度,而是需要在info.plist文件中添加一些參數(shù)(iOS9.0之后),當然我們的軟件想要打開三方的地圖投慈,首先要需要知道三方app中配置的 URL Schemes承耿,然后在我們的info.plist中添加對應(yīng)的字段,如下圖:
info.plist.png 判斷設(shè)備是否安裝對應(yīng)的地圖
既然我們已經(jīng)獲取到了對應(yīng)地圖的 URL Schemes伪煤,那么判斷起來就異常簡單了加袋,以高德舉例
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSLog(@"設(shè)備已安裝高德地圖");
} else {
NSLog(@"設(shè)備未安裝高德地圖");
}
高德地圖
高德SDK文檔中給出的URL如下:
/*
必要參數(shù)的意思:sourceApplication:該app名字; poi name:目的地名稱带族;lat/lon:目的地經(jīng)緯度
dev 參數(shù)進行解釋:dev支持的值為"0"和"1"锁荔,即是否需要進行國測局坐標加密。 如果傳入的坐標已經(jīng)是國測局坐標則傳入0,如果傳入的是地球坐標阳堕,則該參數(shù)傳入1
*/
#define GaoDeNavUrl @"iosamap://navi?sourceApplication=%@&backScheme=myapp&poiname=%@&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=2"
#define GaoDeGPSUrl @"iosamap://viewMap?sourceApplication=%@&poiname=%@&lat=%@&lon=%@&dev=0"
調(diào)用代碼如下:
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleName"];
//backScheme=myapp 這個參數(shù)也可以隨便設(shè)置跋理,如果用高德SDK的話,則需要按照api文檔進行配置
NSString *urlString;
if (self.mode == ShowMapModeGPS) {
urlString = [NSString stringWithFormat:GaoDeGPSUrl, app_Name, self.destination, self.lat, self.lon];
} else {
urlString = [NSString stringWithFormat:GaoDeNavUrl, app_Name, self.destination, self.lat.floatValue, self.lon.floatValue];
}
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
百度地圖
百度SDK文檔中給出的URL如下:
/*
百度地圖的參數(shù)意思就比較簡單了恬总,對mode做一個解釋前普,mode為調(diào)用地圖之后的導(dǎo)航方式,除了walking(步行)還有driving(駕車)和transit(公交)
origin=latlng:0,0 這個參數(shù)雖然意思上是要給一個當前坐標壹堰,但是可以隨意設(shè)置拭卿,這里設(shè)置兩個0,不影響導(dǎo)航
*/
#define BaiDuNavUrl @"baidumap://map/direction?origin=latlng:0,0|name:我的位置&destination=latlng:%@,%@|name:%@&mode=walking"
#define BaiDuGPSUrl @"baidumap://map/marker?location=%@,%@&title=我的位置&content=%@"
調(diào)用代碼如下:
NSString *urlString;
if (self.mode == ShowMapModeGPS) {
urlString = [NSString stringWithFormat:BaiDuGPSUrl, self.lat, self.lon, self.destination];
} else {
urlString = [NSString stringWithFormat:BaiDuNavUrl, self.lat, self.lon, self.destination];
}
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
騰訊地圖
騰訊地圖嘛贱纠,按照官網(wǎng)給的說明是還沒有支持Android和iOS
不過這怎么能難住我們呢峻厚,我們只需要文檔中給的web的URL稍作修改即可,舉個例子谆焊,這個是官方給的地點標注的url
http://apis.map.qq.com/uri/v1/marker?marker=coord:39.892326,116.342763;title:超好吃冰激凌;addr:手帕口橋北鐵路道口&referer=myapp
我們稍作修改惠桃,如下
qqmap://map/marker?marker=coord:39.892326,116.342763;title:超好吃冰激凌;addr:手帕口橋北鐵路道口&referer=myapp
之后調(diào)用方式便跟高德和百度一樣
#define TXGPSUrl @"qqmap://map/marker?marker=coord:%@,%@;title:%@;addr:%@"
NSString *urlString = [NSString stringWithFormat:TXGPSUrl, self.lat, self.lon, self.destination, self.destination];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
結(jié)束語
- 代碼是我封裝好的,上邊代碼中應(yīng)該沒有什么難理解的地方辖试。mode 是一個枚舉辜王,如下
typedef NS_ENUM(NSUInteger, ShowMapMode) {
ShowMapModeGPS, //調(diào)用地圖,只顯示目標點
ShowMapModeNavigation, //調(diào)用地圖罐孝,并直接進行導(dǎo)航
};
- 關(guān)于騰訊地圖:由于騰訊地圖進行導(dǎo)航必須傳入起點坐標呐馆,而系統(tǒng)/高德/百度等地圖不需要,考慮到如果添加騰訊地圖的導(dǎo)航會影響該類的封裝性莲兢,這里便舍棄了騰訊地圖的直接導(dǎo)航功能(也有騰訊地圖用的比較少的原因汹来,個人認為)