地圖在我們 App 開發中已經算是一項比較基本的功能了,但是有些時候不需要我們去集成地圖,而是通過調取本地地圖來實現定位,我上網查了下,完成的能跑通的調用本地地圖的 demo 少之又少,於是就試着自己總結了一下
首先,我們需要導入兩個系統庫文件分別是MapKit.framework,CoreLocation.framework,然後在 plist 文件裏添加如下字段
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
<string>iosamap</string>
<string>comgooglemaps</string>
</array>
因爲我不需要從地圖在跳回到 App 所以URL Types 那也就沒做添加
#import "ViewController.h"
@import CoreLocation;
@import MapKit;
@interface ViewController ()
@property (nonatomic, strong) UIButton *btnSheet;
@property (nonatomic, strong) NSString *urlScheme;
@property (nonatomic, strong) NSString *appName;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.btnSheet = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btnSheet.frame = CGRectMake(0, 0, 100, 50);
self.btnSheet.center = self.view.center;
[self.btnSheet setTitle:@"show sheet" forState:UIControlStateNormal];
[self.btnSheet addTarget:self action:@selector(actionSheet) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btnSheet];
self.urlScheme = @"demoURI://";
self.appName = @"demoURI";
//目標的經緯度(由後臺返給我們)
self.coordinate = CLLocationCoordinate2DMake(0,0);
}
- (void)actionSheet
{
__block NSString *urlScheme = self.urlScheme;
__block NSString *appName = self.appName;
__block CLLocationCoordinate2D coordinate = self.coordinate;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選擇地圖" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//這個判斷其實是不需要的
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
{
UIAlertAction *action = [UIAlertAction actionWithTitle:@"蘋果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
//目的地名稱,可以傳值進來,當然我們實際需要的並不是它,它只作爲給用戶的展示,我們實際需要的是self.coordinate,通過它來進行定位
toLocation.name = @"銀泰·海威國際";
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}];
[alert addAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
{
UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=海威國際&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
{
UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
{
UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action];
}
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end