公司要根据经纬度在地图上显示一些东西,本来都是百度地图做的,这次想试试集成下高德地图看看.
1.配置环境
(1)注册高德账号,创建地图应用,生成唯一的APIKey;
(2)使用cocoapods,将高德地图的在工程中添加即可,
创建Podfile
touch Podfile
编辑Podfile
vim Podfile
在Podfile中写入以下数据
platform :ios, ‘8.0‘
target "gaoMap" do
pod 'AMap2DMap', '~> 4.0.0'
end
保存退出,然后安装
pod install
等待后会有成功导入的提示.然后重新打开生成的XXX.xcworkspace即可
2.集成基本的2D地图功能,并放置大头针功能
(1)在AppDelegate.m文件中加入#import<AMapFoundationKit/AMapFoundationKit.h>头文件,后在添加[AMapServices sharedServices].apiKey = ApiKey;
(2)修改.plist文件的相关属性,App Transport Security Settings下的Allow Arbitrary Loads属性改为YES.
(3)在ViewController.m文件中引入头文件:#import<MAMapKit/MAMapKit.h>
再在
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// Do any additional setup after loading the view, typically from a nib.
_mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
_mapView.delegate = self;
[self.view addSubview:_mapView];
//放置大头针
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
//地图中心点
_mapView.centerCoordinate = CLLocationCoordinate2DMake(32.041675,118.767568);
pointAnnotation.coordinate = CLLocationCoordinate2DMake(32.041675,118.767568);
//地图放大级别
_mapView.zoomLevel = 18;
//大头针内容
pointAnnotation.title = @"呵呵";
pointAnnotation.subtitle = @"哈哈";
[_mapView addAnnotation:pointAnnotation];
}
//调用大头针的方法
- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation;
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
MAPinAnnotationView *annotationView = nil;
if (annotationView == nil)
{
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
}
annotationView.pinColor = MAPinAnnotationColorGreen;
annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO
//annotationView.image = [UIImage imageNamed:@"restaurant"];
annotationView.animatesDrop = NO; //设置标注动画显示,默认为NO
annotationView.draggable = YES; //设置标注可以拖动,默认为NO
return annotationView;
}
return nil;
}
以上基本可以显示大头针所在位置和点击大头针显示相关提示信息的界面了,简单的集成就到此结束了.