ios调用百度、高德地图 导航功能
要调用第三方app,先要在info.plist中 LSApplicationQueriesSchemes 中增加 app
百度地图:baidumap
高德地图:iosamap
#pragma mark - 导航
-(void) gotoStoreShop
{
bool hasBaiduMap = NO;
bool hasGaodeMap = NO;
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
hasBaiduMap = YES;
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
hasGaodeMap = YES;
}
if (hasGaodeMap && hasBaiduMap) {
UIBAlertView *alert = [[UIBAlertView alloc] initWithTitle:@"提醒" message:@"请选择导航工具!" cancelButtonTitle:@"取消" otherButtonTitles:@"百度地图",@"高德地图",nil];
[alert showWithDismissHandler:^(NSInteger selectedIndex, NSString *selectedTitle, BOOL didCancel) {
if (didCancel) {
return;
}
switch (selectedIndex) {
case 1://百度地图
[self startBaiduMap];
break;
case 2://高德地图
[self startGaodeMap];
break;
default:
break;
}
}];
} else if (hasBaiduMap){
[self startBaiduMap];
} else if (hasGaodeMap){
[self startGaodeMap];
} else {
ALERT(@"请先安装百度地图或高德地图,才能使用导航功能!");
}
}
-(void) startGaodeMap
{
CLLocationCoordinate2D myLocation = [LocationManager shareManager].location.coordinate;
CLLocationCoordinate2D storeLoacation = curStoreLoacal;
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0",@"我的应用", myLocation.latitude, myLocation.longitude,@"我的位置", storeLoacation.latitude, storeLoacation.longitude, storeName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
}
-(void) startBaiduMap
{
CLLocationCoordinate2D myLocation = [LocationManager shareManager].location.coordinate;
CLLocationCoordinate2D storeLoacation = curStoreLoacal;
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:%@&destination=latlng:%f,%f|name:%@&mode=driving", myLocation.latitude, myLocation.longitude, @"我的位置", storeLoacation.latitude, storeLoacation.longitude, storeName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
}
附加一个自定义UIAlertView选择地图
UIBAlertView.h
//
// UIBAlertView.h
// TianYiXinForPatient
//
// Created by huangfeng on 16/3/30.
// Copyright © 2016年 Lin. All rights reserved.
//
#import <Foundation/Foundation.h>
@class UIBAlertView;
typedef void (^UIBAlertDismissedHandler) (NSInteger selectedIndex, NSString *selectedTitle, BOOL didCancel);
typedef BOOL (^UIBAlertShouldEnableFirstOtherButtonHandler)();
@interface UIBAlertView : NSObject
@property (copy, nonatomic) UIBAlertShouldEnableFirstOtherButtonHandler shouldEnableFirstOtherButtonHandler;
// UIAlertView passthroughs
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);
// UIBAlertView functionality
- (id)initWithTitle:(NSString *)aTitle message:(NSString *)aMessage cancelButtonTitle:(NSString *)aCancelTitle otherButtonTitles:(NSString *)otherTitles,...NS_REQUIRES_NIL_TERMINATION;
- (void)showWithDismissHandler:(UIBAlertDismissedHandler)handler;
@end
UIBAlertView.m
//
// UIBAlertView.m
// TianYiXinForPatient
//
// Created by huangfeng on 16/3/30.
// Copyright © 2016年 Lin. All rights reserved.
//
#import "UIBAlertView.h"
@interface UIBAlertView() <UIAlertViewDelegate>
@property (strong, nonatomic) UIBAlertView *strongAlertReference;
@property (copy, nonatomic) UIBAlertDismissedHandler activeDismissHandler;
@property (strong, nonatomic) UIAlertView *activeAlert;
@end
@implementation UIBAlertView
#pragma mark - Public (Initialization)
- (id)initWithTitle:(NSString *)aTitle message:(NSString *)aMessage cancelButtonTitle:(NSString *)aCancelTitle otherButtonTitles:(NSString *)otherTitles,... {
self = [super init];
if (self) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:aTitle message:aMessage delegate:self cancelButtonTitle:aCancelTitle otherButtonTitles:otherTitles, nil];
if (otherTitles != nil) {
va_list args;
va_start(args, otherTitles);
NSString * title = nil;
while((title = va_arg(args,NSString*))) {
[alert addButtonWithTitle:title];
}
va_end(args);
}
self.activeAlert = alert;
}
return self;
}
#pragma mark - Public (Functionality)
- (void)showWithDismissHandler:(UIBAlertDismissedHandler)handler {
self.activeDismissHandler = handler;
self.strongAlertReference = self;
[self.activeAlert show];
}
#pragma mark UIAlertView passthroughs
- (UIAlertViewStyle)alertViewStyle
{
return self.activeAlert.alertViewStyle;
}
- (void)setAlertViewStyle:(UIAlertViewStyle)alertViewStyle
{
self.activeAlert.alertViewStyle = alertViewStyle;
}
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex
{
return [self.activeAlert textFieldAtIndex:textFieldIndex];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (self.activeDismissHandler) {
self.activeDismissHandler(buttonIndex, [alertView buttonTitleAtIndex:buttonIndex], buttonIndex == alertView.cancelButtonIndex);
}
self.strongAlertReference = nil;
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (self.shouldEnableFirstOtherButtonHandler)
return self.shouldEnableFirstOtherButtonHandler();
return YES;
}
@end