公司产品需要用到地图功能,不过由于只是展示地图上的位置信息,所以没有集成地图,改为跳转到高德或者百度地图,如果都没有就跳转到safari上的网页地图。
- 第一步. 首先配置infoPlist
//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不管有没有安装高德地图值都是YES, 所以是否打开成功不能用这个参数判断
//所以在appdelegate里面添加了参数openingExternalProgram判断是否有打开高德或者百度
if (((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram) {
[ZMLoad stopLoading];
((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram = NO;
LCLog(@"跳转高德地图成功");
}else{
LCLog(@"跳转高德地图失败");
[[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(@"跳转百度地图成功");
}else{
[ZMLoad stopLoading];
LCLog(@"跳转百度地图失败");
[self loadSafari];
}
}];
}
}];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self getAmapUrl:request]]];
[LCTools delay:0.5 completeHandle:^{
if (((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram)
{
NSLog(@"跳转高德地图成功");
[ZMLoad stopLoading];
((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram = NO;
}
else
{
NSLog(@"跳转高德地图失败");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self getBaiduMapUrl:request]]];
[LCTools delay:0.5 completeHandle:^{
if (((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram)
{
NSLog(@"跳转百度地图成功");
[ZMLoad stopLoading];
((AppDelegate *)[UIApplication sharedApplication].delegate).openingExternalProgram = NO;
}
else
{
NSLog(@"跳转百度地图失败");
[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名称"),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名称"),urlencode(@"gcj02")];
NSLog(@"%@",url);
return url;
}
//这行代码是AppDelegate里面的---
- (void)applicationWillResignActive:(UIApplication *)application {
self.openingExternalProgram = YES;
}