美文网首页IOS
iOS根据地址在地图上展示坐标

iOS根据地址在地图上展示坐标

作者: 枫叶风 | 来源:发表于2017-04-24 16:11 被阅读31次

    遇到一个需求,给出起点和终点的位置,需要在地图中展示出来,并有导航功能。
    首先需要把位置转化成经纬度,这个可以使用系统提供的方法,需要引入框架#import <MapKit/MapKit.h>

        // address为具体地址名称
        NSString *address;
        [myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if ([placemarks count] > 0 && error == nil) {
                CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
                // coord即为得到到坐标
                CLLocationCoordinate2D coord;
                coord.latitude = firstPlacemark.location.coordinate.latitude;
                coord.longitude = firstPlacemark.location.coordinate.longitude;
                // 由于项目中使用到是百度地图,需要把坐标转化为百度地图到坐标
                CLLocationCoordinate2D baiduCoor;
                baiduCoor = [self changeBaiDuGPS:coord];
            } else if ([placemarks count] == 0 && error == nil) {
                NSLog(@"Found no placemarks.");
            } else if (error != nil) {
                NSLog(@"An error occurred = %@", error);
            }
        }];
    
        // 坐标转化方法
        - (CLLocationCoordinate2D)changeBaiDuGPS:(CLLocationCoordinate2D)coor {
        //转换国测局坐标(google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标)至百度坐标
        NSDictionary* testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_COMMON);
        //转换后的百度坐标
        CLLocationCoordinate2D baiduCoor = BMKCoorDictionaryDecode(testdic);
        
        return baiduCoor;
        }
    

    同样通过上面的方法可以得到起始点的坐标和终点的坐标,把坐标展示到地图上:

        startPoint = [[BMKPointAnnotation alloc] init];
        startPoint.coordinate = startCoor;
        [_mapView addAnnotation:startPoint];
    
        endPoint = [[BMKPointAnnotation alloc] init];
        endPoint.coordinate = endCoor;
        [_mapView addAnnotation:endPoint];
    

    大头针已经添加到地图上了,下面需要自定义大头针的样式,需要添加百度地图代理_mapView.delegate = self;

    // BMKMapViewDelegate
    - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
        if (annotation == startPoint) {
            NSString *AnnotationViewID = @"renameMark";
            BMKAnnotationView *annotationView = (BMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
            annotationView.frame = CGRectMake(0, 0, 30, 30);
            
            if (annotationView == nil) {
                annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
                annotationView.image = [UIImage imageNamed:@"map_start"];
            }
            return annotationView;
        } else if (annotation == endPoint) {
            NSString *AnnotationViewID = @"renameMark";
            BMKAnnotationView *annotationView = (BMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
            annotationView.frame = CGRectMake(0, 0, 30, 30);
            if (annotationView == nil) {
                annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
                annotationView.image = [UIImage imageNamed:@"map_end"];
            }
            return annotationView;
        }
        return nil;
    }
    
    自定义大头针样式
    页面展示基本上完成了,导航功能需要跳转到相应app就可以,目前我只做了跳转到百度地图和系统自带地图,跳转相应app需要在plist文件中设置
    plist文件设置相应选项
    跳转选项使用UIAlertController
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [alertController addAction:[UIAlertAction actionWithTitle:@"使用苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self jumpAppleMap];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"使用百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self jumpBaiduMap];
        }]];
        [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
        [self presentViewController: alertController animated: YES completion: nil];
    

    跳转到苹果地图,此时使用的坐标为系统根据文字转换的坐标,不必使用百度地图的坐标:

        - (void)jumpAppleMap {
        MKMapItem *startLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:iosStartCoor addressDictionary:nil]];
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:iosEndCoor addressDictionary:nil]];
        [MKMapItem openMapsWithItems:@[startLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        }
    

    跳转到百度地图,此时需要使用转化后的百度地图坐标:

    - (void)jumpBaiduMap {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {
            NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&&mode=driving",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];
            NSURL *url = [NSURL URLWithString:urlString];
            
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        } else {
            // 没有安装百度地图,无法打开
        }
    }
    

    这样程序便会跳转到相应APP,并进入到导航功能。

    相关文章

      网友评论

        本文标题:iOS根据地址在地图上展示坐标

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