I love iOSReportsios框架

iOS 无需SDK,一行代码调用主流地图

2016-03-19  本文已影响2807人  奴良

在开发中,有时候我们并不需要太过于复杂的地图功能,或许只需要调用系统地图、高德、百度等主流地图就可以完成我们所需要的功能。比如调用三方地图进行一个线路的导航、或者进行地点的标注。那么下面将会为大家介绍如何不集成高德、百度等地图的SDK,达到在项目中调用其的效果。

调用系统地图

调用iPhone系统地图的话,我们需要导入一个框架,即: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];
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]}];

关于上面这个字典的参数,给出如下注释:

/**
         MKLaunchOptionsDirectionsModeKey   路线模式,常量
         MKLaunchOptionsDirectionsModeDriving  驾车模式
         MKLaunchOptionsDirectionsModeWalking 步行模式
         
         MKLaunchOptionsMapTypeKey  地图类型,
         枚举 MKMapTypeStandard :标准模式
         MKMapTypeSatellite :卫星模式
         MKMapTypeHybrid  :混合模式
         
         MKLaunchOptionsMapCenterKey 中心点坐标, CLLocationCoordinate2D类型
         MKLaunchOptionsMapSpanKey  地图显示跨度,MKCoordinateSpan 类型
         MKLaunchOptionsShowsTrafficKey 是否 显示交通状况,布尔型
         MKLaunchOptionsCameraKey   3D地图效果,MKMapCamera类型(注意:此属性从iOS7及以后可用,前面的属性从iOS6开始可用)
         */

效果图:


Map.PNG

调用三方地图的准备工作

当然除了系统地图,三方地图的开发文档上也都给的有说明,这里不详细解释对应的参数,下面代码中只挑一些必要的进行解释。

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSLog(@"设备已安装高德地图");
    } else {
        NSLog(@"设备未安装高德地图");
    }

高德地图

高德SDK文档中给出的URL如下:

/*
必要参数的意思:sourceApplication:该app名字; poi name:目的地名称;lat/lon:目的地经纬度
dev 参数进行解释:dev支持的值为"0"和"1",即是否需要进行国测局坐标加密。 如果传入的坐标已经是国测局坐标则传入0,如果传入的是地球坐标,则该参数传入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"

调用代码如下:

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleName"];
//backScheme=myapp 这个参数也可以随便设置,如果用高德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如下:

/*
百度地图的参数意思就比较简单了,对mode做一个解释,mode为调用地图之后的导航方式,除了walking(步行)还有driving(驾车)和transit(公交)
origin=latlng:0,0  这个参数虽然意思上是要给一个当前坐标,但是可以随意设置,这里设置两个0,不影响导航
*/
#define BaiDuNavUrl @"baidumap://map/direction?origin=latlng:0,0|name:我的位置&destination=latlng:%@,%@|name:%@&mode=walking"
#define BaiDuGPSUrl @"baidumap://map/marker?location=%@,%@&title=我的位置&content=%@"

调用代码如下:

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];

腾讯地图

腾讯地图嘛,按照官网给的说明是还没有支持Android和iOS


TX.png

不过这怎么能难住我们呢,我们只需要文档中给的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

之后调用方式便跟高德和百度一样

#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];

结束语

typedef NS_ENUM(NSUInteger, ShowMapMode) {
    ShowMapModeGPS, //调用地图,只显示目标点
    ShowMapModeNavigation, //调用地图,并直接进行导航
};
上一篇下一篇

猜你喜欢

热点阅读