美文网首页iOS开发技巧
打开地图以及传入关键词搜索

打开地图以及传入关键词搜索

作者: 夏天的枫_ | 来源:发表于2019-04-03 14:29 被阅读0次

// 打开地图,传入搜索关键词
-(void)nearbyMaintainPressed:(id)sender
{

//判断手机的系统,8.0以上使用UIAlertController,一下使用UIAlertView
//    if (IAIOS8) {

self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
alert.view.tintColor = [UIColor blackColor];

//通过拍照上传图片
UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"百度地图搜索" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSString *urlString = [@"baidumap://map/nearbysearch?query=汽车维修&radius=1000&src=" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
    else {
        [_APP showErrorMsg:@"还未安装百度地图"];
    }
    
}];
//从手机相册中选择上传图片
UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"高德地图搜索" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSString *urlString = [@"iosamap://poi?sourceApplication=xxx&name=汽车维修" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        //iOS10以后,使用新API
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]
                                           options:@{}
                                 completionHandler:^(BOOL success) {
                                     NSLog(@"scheme调用结束");
                                 }
         ];
    }
    else {
        [_APP showErrorMsg:@"还未安装高德地图"];
    }
    
}];

UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
}];

[alert addAction:cameraAction];
[alert addAction:photoAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];

}
// 单纯只是打开地图
-(void)navPressed:(id)sender
{

//判断手机的系统,8.0以上使用UIAlertController,一下使用UIAlertView
//    if (IAIOS8) {

self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
alert.view.tintColor = [UIColor blackColor];

//
UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"百度地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",[_APP.defaultVehicleData.lat floatValue], [_APP.defaultVehicleData.lng floatValue]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
    else {
        [_APP showErrorMsg:@"还未安装百度地图"];
    }
    
}];

UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"高德地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"车保姆",@"DriveInfoPrj://",[_APP.defaultVehicleData.lat floatValue], [_APP.defaultVehicleData.lng floatValue]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
    else {
        [_APP showErrorMsg:@"还未安装高德地图"];
    }
    
}];

UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
}];

[alert addAction:cameraAction];
[alert addAction:photoAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];

}

相关文章

网友评论

    本文标题:打开地图以及传入关键词搜索

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