公司產(chǎn)品需要用到地圖功能舟铜,不過(guò)由于只是展示地圖上的位置信息,所以沒(méi)有集成地圖,改為跳轉(zhuǎn)到高德或者百度地圖屯曹,如果都沒(méi)有就跳轉(zhuǎn)到safari上的網(wǎng)頁(yè)地圖。
- 第一步. 首先配置infoPlist
info_plist.png
//ios10以后OpenURL要用新方法
if ([[UIDevice currentDevice].systemVersion integerValue] >= 10)
{
NSDictionary *options = @{UIApplicationOpenURLOptionsSourceApplicationKey:@YES};
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self getAmapUrl:request]] options:options completionHandler:^(BOOL success) {
//這里的success不管有沒(méi)有安裝高德地圖值都是YES, 所以是否打開(kāi)成功不能用這個(gè)參數(shù)判斷
//所以在appdelegate里面添加了參數(shù)openingExternalProgram判斷是否有打開(kāi)高德或者百度
if (((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram) {
[ZMLoad stopLoading];
((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram = NO;
LCLog(@"跳轉(zhuǎn)高德地圖成功");
}else{
LCLog(@"跳轉(zhuǎn)高德地圖失敗");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self getBaiduMapUrl:request]] options:options completionHandler:^(BOOL success) {
if (((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram) {
[ZMLoad stopLoading];
((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram = NO;
LCLog(@"跳轉(zhuǎn)百度地圖成功");
}else{
[ZMLoad stopLoading];
LCLog(@"跳轉(zhuǎn)百度地圖失敗");
[self loadSafari];
}
}];
}
}];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self getAmapUrl:request]]];
[LCTools delay:0.5 completeHandle:^{
if (((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram)
{
NSLog(@"跳轉(zhuǎn)高德地圖成功");
[ZMLoad stopLoading];
((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram = NO;
}
else
{
NSLog(@"跳轉(zhuǎn)高德地圖失敗");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self getBaiduMapUrl:request]]];
[LCTools delay:0.5 completeHandle:^{
if (((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram)
{
NSLog(@"跳轉(zhuǎn)百度地圖成功");
[ZMLoad stopLoading];
((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram = NO;
}
else
{
NSLog(@"跳轉(zhuǎn)百度地圖失敗");
[ZMLoad stopLoading];
[self loadSafari];
}
}];
}
}];
}
}
- (void)loadSafari
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self getSafariUrl]]];
}
- (NSString *)getSafariUrl
{
if (self.request==nil)
{
return @"";
}
NSString *coordinate = [self.request.URL queryParameterForKey:@"coordinate"];
NSString *position = [self.request.URL queryParameterForKey:@"position"];
NSString *name = [self.request.URL queryParameterForKey:@"name"];
NSString *url = [NSString stringWithFormat:@"http://uri.amap.com/marker?callnative=0&coordinate=%@&position=%@&name=%@",coordinate,position,urlencode(name)];
return url;
}
- (NSString *)getAmapUrl:(NSURLRequest *)request
{
LCLog(@"%@",urldecode([request.URL queryParameterForKey:@"name"]));
NSString *name = [request.URL queryParameterForKey:@"name"];
NSString *position = [request.URL queryParameterForKey:@"position"];
NSArray *positionArr = [position componentsSeparatedByString:@","];
NSString *lon = [positionArr firstObject];
NSString *lat = [positionArr lastObject];
NSString *url = [NSString stringWithFormat:@"iosamap://viewMap?sourceApplication=%@&poiname=%@&lat=%@&lon=%@&dev=1",urlencode(@"app名稱(chēng)"),urlencode(name),lat,lon];
return url;
}
- (NSString *)getBaiduMapUrl:(NSURLRequest *)request
{
NSString *name = [request.URL queryParameterForKey:@"name"];
NSString *position = [request.URL queryParameterForKey:@"position"];
NSArray *positionArr = [position componentsSeparatedByString:@","];
NSString *lon = [positionArr firstObject];
NSString *lat = [positionArr lastObject];
// NSString *url = @"baidumap://";
NSString *url = [NSString stringWithFormat:@"baidumap://map/marker?location=%@,%@&title=%@&content=%@&src=%@&coord_type=%@",lat,lon,urlencode(name),urlencode(name),urlencode(@"webapp.geo.app名稱(chēng)"),urlencode(@"gcj02")];
NSLog(@"%@",url);
return url;
}
//這行代碼是AppDelegate里面的---
- (void)applicationWillResignActive:(UIApplication *)application {
self.openingExternalProgram = YES;
}