美文网首页iOS工具和框架
IOS选择跳转导航地图,未安装跳到AppStore

IOS选择跳转导航地图,未安装跳到AppStore

作者: 蜗牛1992 | 来源:发表于2016-08-16 22:42 被阅读366次
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少

- (void)ClickNavButton
{
    UIAlertController *pAlertController = [UIAlertController alertControllerWithTitle:@"选择导航地图" message:@"若未安装跳到AppStore下载" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *pAlertAppleAction = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        m_pNavImageView.hidden = NO;
        [self openAppleMap];
    }];
    UIAlertAction *pAlertBaiduAction = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        m_pNavImageView.hidden = NO;
        [self openBaiduMap];
    }];
    UIAlertAction *pAlertGaodeAction = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        m_pNavImageView.hidden = NO;
        [self openGaodeMap];
    }];
    UIAlertAction *alertCancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        m_pNavImageView.hidden = NO;
    }];
    [pAlertController addAction:pAlertAppleAction];
    [pAlertController addAction:pAlertBaiduAction];
    [pAlertController addAction:pAlertGaodeAction];
    [pAlertController addAction:alertCancelAction];
    [self presentViewController:pAlertController animated:YES completion:^{
        m_pNavImageView.hidden = YES;
    }];
}

-(void)openAppleMap{
    //获取当前位置
    MKMapItem *pMylocation = [MKMapItem mapItemForCurrentLocation];
    
    //当前经维度
    float pCurrentLatitude= pMylocation.placemark.location.coordinate.latitude;
    float pCurrentLongitude= pMylocation.placemark.location.coordinate.longitude;
    
    m_pcurrentCoords = CLLocationCoordinate2DMake(pCurrentLatitude,pCurrentLongitude);
    
    // ios6以下,调用google map
    if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
    {
        NSString *pstrUrlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d", m_pcurrentCoords.latitude,m_pcurrentCoords.longitude,m_pToLocationCoord.latitude,m_pToLocationCoord.longitude];
        NSURL *pURL = [NSURL URLWithString:pstrUrlString];
        //打开网页google地图
        [[UIApplication sharedApplication] openURL:pURL];
    }
    else
        // 直接调用ios自己带的apple map
    {
        //当前的位置
        MKMapItem *pCurrentLocation = [MKMapItem mapItemForCurrentLocation];
        //目的地的位置
        MKMapItem *pToLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:m_pToLocationCoord addressDictionary:nil]];
        pToLocation.name = m_pstrToLocationName; //要显示目的地名字
        NSArray *items = [NSArray arrayWithObjects:pCurrentLocation, pToLocation, nil];
        NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
        //打开苹果自身地图应用,并呈现特定的item
        [MKMapItem openMapsWithItems:items launchOptions:options];
    }
}

-(void)openBaiduMap{

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]==YES){
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving",m_pcurrentCoords.latitude,m_pcurrentCoords.longitude,m_pToLocationCoord.latitude,m_pToLocationCoord.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
    }else{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/bai-du-tu-shou-ji-tu-lu-xian/id452186370?mt=8&v0=WWW-GCCN-ITSTOP100-FREEAPPS&l=&ign-mpt=uo%3D4"]];
    }
}

-(void)openGaodeMap{
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]==YES){
    NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2",@"app name", @"YGche", @"终点", m_pToLocationCoord.latitude, m_pToLocationCoord.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
    }else{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/gao-tu-zhuan-ye-shou-ji-tu/id461703208?mt=8"]];
    }
}

相关文章

网友评论

    本文标题:IOS选择跳转导航地图,未安装跳到AppStore

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