ios调用百度、高德地图 导航功能

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值