美文网首页
iOS地图导航功能实现

iOS地图导航功能实现

作者: CocoaH | 来源:发表于2017-08-02 16:48 被阅读57次

最简单快捷的方法使用高德地图uri,高德地图uri的具体使用可在高德地图官方网站看,使用非常简单

简单贴下部分代码

   NSString *urlStr = [NSString stringWithFormat:@"http://uri.amap.com/navigation?from=%f,%f,我的位置&to=%@,%@,%@&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0",self.currentLocation.coordinate.longitude,self.currentLocation.coordinate.latitude,model.lng,model.lat,model.swjg_mc];
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString encodeToPercentEscapeString: urlStr]]];
    [self.webView loadRequest:request];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
效果图.png

这种实现方式,点击导航的时候可以设置直接跳转到高德地图,但是如果用户没有安装高德地图的话,就直接跳转到App Store ,用户体验不是很好
所以后来需求改成,点击了立即导航让选择使用本地已经安装的地图

D6943629-6BA3-4D12-A048-C0D848E9A370.png

那么现在问题出现了,立即导航的按钮是高德web页面的方法,iOS 怎么监听事件呢,于是就搜索了各种方法,最后选择通过UIWebView 的代理方法监听

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

webview 每次加载都会调用这个方法,可以通过request.url 来判断用户是否点击了立即导航按钮

代码如下

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = [request URL];

    BOOL returnValue = YES;
    if ([url.absoluteString containsString:@"iosamap"] || [url.absoluteString containsString:@"amapuri://route/plan/"]) {
        NSLog(@"点击了导航======================%@",url.absoluteString);
        [self showSheetView];
       //注意不要直接  return NO;
        returnValue = NO;
    }
    if ([url.absoluteString containsString:@"wap.amap.com"]) {
        returnValue = NO;
    }
    return returnValue;
}

立即导航的点击事件截取到之后,然后,在通过[UIApplication sharedApplication] canOpenURL: 方法来判断本地安装了哪些地图
高德地图:iosamap://
百度地图:baidumap://

高德,百度 ,苹果地图的跳转方法:本文中跳转到地图显示效果是这样的,需要传入终点的经纬度

91CC416BB73A4547583E3D6FD6DA6863.png
//高德地图
- (void)openGaoDeMap{
    NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%f&slon=%f&sname=我的位置&did=BGVIS2&dlat=%@&dlon=%@&dname=%@&dev=0&t=0",self.currentLocationLatitude,self.currentLocationLongitude,self.lat,self.lng,self.addressName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *myLocationScheme = [NSURL URLWithString:urlString];
    if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
        [[UIApplication sharedApplication] openURL:myLocationScheme options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束"); }];
    } else { //iOS10以前,使用旧API
        [[UIApplication sharedApplication] openURL:myLocationScheme];
    }
    
}
//百度地图
- (void)openBaiduMap {
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:BAIDUMAP]]) {
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name:%@&mode=driving&coord_type=bd09&origin_region=%@&destination_region=%@",self.lat, self.lng,self.addressName,@"",@""]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        } else {
            [ToolKit showAlertMessage:@"" currentVc:self];
        }
    }
}

//苹果地图
- (void)openAppleMap {
//出发点的经纬度可以不传,默认是当前位置
//终点的位置
    CLLocationCoordinate2D tolocation = CLLocationCoordinate2DMake(纬度, 经度);
    MKMapItem *currentLocationItem = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocationItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:tolocation addressDictionary:@{}]];
    toLocationItem.name = self.addressName;
    [MKMapItem openMapsWithItems:@[currentLocationItem, toLocationItem]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey:
                    MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
    
}

相关文章

  • iOS Core Location 之 定位 与 地理编码

    一、定位功能简介 要实现地图、导航功能,往往需要先熟悉定位功能。在 iOS 中通过 Core Location 框...

  • iOS地图导航功能实现

    最简单快捷的方法使用高德地图uri,高德地图uri的具体使用可在高德地图官方网站看,使用非常简单 简单贴下部分代码...

  • iOS 集成高德地图

    参考文档:iOS 跳转方式实现地图导航功能 应用内导航 是指使用地图服务提供的SDK(比如高德,百度等等),直接将...

  • iOS 实战笔记5-(Geekband)

    要实现地图、导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作。Core ...

  • iOS定位和位置信息获取

    要实现地图、导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作。Core ...

  • 实用技术——地图_CoreLocation_定位1

    导读 要实现地图、导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作。Co...

  • Core Location (用于地理定位)和MapKit系列

    iOS开发中想要实现导航(去陌生的地方),周边(找餐厅,酒店,银行等)等功能,都有用到地图和定位功能.要想实现这两...

  • 11.2 苹果原生地图

    一、定位要实现地图、导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作。C...

  • 地图导航

    URI跳转方式地图导航的代码实践iOS调用第三方地图路线导航IOS实现应用内打开第三方地图app进行导航 高德 i...

  • iOS地图定位【学习版】

    首先要实现地图、导航功能,就需要我们先熟悉定位功能,在iOS中通过Core Location框架进行定位操作。Co...

网友评论

      本文标题:iOS地图导航功能实现

      本文链接:https://www.haomeiwen.com/subject/ghqylxtx.html