-(void)clickDH:(UIButton *)btn{
NearbyGasModel *model=self.dataArray[btn.tag];
CLLocationDegrees longitude = [NSString stringWithFormat:@"%@",model.lon].floatValue;
CLLocationDegrees latitude = [NSString stringWithFormat:@"%@",model.lat].floatValue;
CLLocationCoordinate2D endLocation = CLLocationCoordinate2DMake(latitude, longitude);
NSArray *locationArr = [model.position componentsSeparatedByString:@","];
CLLocationDegrees googlelongitude = [NSString stringWithFormat:@"%@",locationArr[1]].floatValue;
CLLocationDegrees googlelatitude = [NSString stringWithFormat:@"%@",locationArr[0]].floatValue;
CLLocationCoordinate2D googleLocation = CLLocationCoordinate2DMake(googlelongitude,googlelatitude);
NSLog(@"lon:%f,lat:%f",longitude,latitude);
//endLocation要跳转的经纬度-百度的 googleLocation要跳转的经纬度-谷歌的,高德,腾讯,苹果
[self navThirdMapWithLocation:endLocation GoogleLocation:googleLocation andTitle:[NSString stringWithFormat:@"%@",model.name]];
}
///服务器给的数据中,经纬度是百度的lon,lat,其他的要用谷歌的经纬度position,
-(void)navThirdMapWithLocation:(CLLocationCoordinate2D)endLocation GoogleLocation:(CLLocationCoordinate2D)googleLocation andTitle:(NSString *)titleStr{
NSMutableArray *mapsA = [NSMutableArray array];
//苹果原生地图方法和其他不一样
NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
iosMapDic[@"title"] = @"苹果地图";
[mapsA addObject:iosMapDic];
//高德地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
gaodeMapDic[@"title"] = @"高德地图";
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=ios.blackfish.XHY&dlat=%f&dlon=%f&dname=%@&style=2",googleLocation.latitude,googleLocation.longitude,titleStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
gaodeMapDic[@"url"] = urlString;
[mapsA addObject:gaodeMapDic];
}
//百度地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
baiduMapDic[@"title"] = @"百度地图";
//百度自己的SDK获取的经纬度要用bd09ll,,,其他的一般用gcj02
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name:%@&coord_type=bd09ll&mode=driving&src=ios.blackfish.XHY",endLocation.latitude,endLocation.longitude,titleStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
baiduMapDic[@"url"] = urlString;
[mapsA addObject:baiduMapDic];
//腾讯地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
qqMapDic[@"title"] = @"腾讯地图";
NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&to=%@&tocoord=%f,%f&coord_type=1&referer={ios.blackfish.XHY}",titleStr,googleLocation.latitude,googleLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
qqMapDic[@"url"] = urlString;
[mapsA addObject:qqMapDic];
}
}
//手机地图个数判断
if (mapsA.count > 0) {
//选择
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"使用导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSInteger index = mapsA.count;
for (int i = 0; i < index; i++) {
NSString *title = mapsA[i][@"title"];
NSString *urlString = mapsA[i][@"url"];
if (i == 0) {
UIAlertAction *iosAntion = [UIAlertAction actionWithTitle:title style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
[self appleNaiWithCoordinate:googleLocation andWithMapTitle:titleStr];
}];
[alertVC addAction:iosAntion];
continue;
}
UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alertVC addAction:action];
}
UIAlertAction *cancleAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:cancleAct];
[self presentViewController:alertVC animated:YES completion:^{
}];
}else{
}
}
//唤醒苹果自带导航
- (void)appleNaiWithCoordinate:(CLLocationCoordinate2D)coordinate andWithMapTitle:(NSString *)map_title{
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
tolocation.name = map_title;
[MKMapItem openMapsWithItems:@[currentLocation,tolocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}