ios项目中调用百度、高德、本机地图导航(二)
发布时间
阅读量:
阅读量
话不多说直接上代码:
本机地图
第一步 准备工作
导入<MapKit/MapKit.h> <CoreLocation/CoreLocation.h> 两个库
第二步 代码部分
在需要调用的.h文件中加入
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
AI写代码
然后调用代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"启动本机地图" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//百度地图
[self startiosMap];
}];
[alert addAction:cancel];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
AI写代码
//本机地图
-(void) startiosMap
{
CLLocationCoordinate2D myLocation = CLLocationCoordinate2DMake(29.569389,106.557978);
CLLocationCoordinate2D storeLoacation = CLLocationCoordinate2DMake(29.615133,106.605053);
//高德的坐标转换接口
myLocation = AMapCoordinateConvert(myLocation, AMapCoordinateTypeGPS);
storeLoacation =AMapCoordinateConvert(storeLoacation, AMapCoordinateTypeGPS);
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:storeLoacation addressDictionary:nil]];
toLocation.name = @“美心洋人街”;
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}
AI写代码
AI写代码
全部评论 (0)
还没有任何评论哟~
