一:百度地图
1.按照API文档导入相应的库
2.AppDelegate文件
#import "BMapKit.h" BMKMapManager* _mapManager; _mapManager = [[BMKMapManager alloc]init]; BOOL ret = [_mapManager start:@"please enter your key" generalDelegate:self]; if (!ret) { NSLog(@"manager start failed!"); }
3.对应的viewController文件
#import "MapViewController.h" #import "BMapKit.h" #import "MyAnontation.h" @interface MapViewController ()<BMKMapViewDelegate> { BMKMapView *_mapView; } @end @implementation MapViewController - (void)viewDidLoad { [super viewDidLoad]; [self createBackBtn]; self.view.backgroundColor = [UIColor whiteColor]; _mapView=[[BMKMapView alloc]initWithFrame:self.view.bounds]; [self.view addSubview:_mapView]; [_mapView viewWillAppear]; _mapView.centerCoordinate=CLLocationCoordinate2DMake(_cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue); _mapView.delegate=self; _mapView.zoomLevel=14; //BMKPointAnnotation *annotation=[[BMKPointAnnotation alloc]init]; //annotation.coordinate=_mapView.centerCoordinate; //[_mapView addAnnotation:annotation]; CLLocationCoordinate2D coord; coord.longitude=[_cinemaModel.longitude doubleValue]; coord.latitude=[_cinemaModel.latitude doubleValue]; //自定义一个大头针 MyAnontation *anontation=[[MyAnontation alloc] initWithCoor:coord title:_cinemaModel.name subTitle:_cinemaModel.addr]; //大头针的编号 //anontation.anonIndex=i; //添加到地图上(使用添加大头针的方法,不是添加控件) [_mapView addAnnotation:anontation]; } #pragma mark --BMKMapViewDelegate代理---- - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation { BMKAnnotationView *view=[[BMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"annotation"]; view.image=[UIImage imageNamed:@"mark"]; return view; }
二:高德地图
1.AppDelegate设置key值(高德后台注册)
#import <MAMapKit/MAMapKit.h>
[MAMapServices sharedServices].apiKey = (NSString *)key;
2.对应的viewController文件
#import "MAMapViewController.h" #import <MAMapKit/MAMapKit.h> @interface MAMapViewController ()<MAMapViewDelegate> { MAMapView *_mapView; } @end @implementation MAMapViewController - (void)viewDidLoad { [super viewDidLoad]; [self createBackBtn]; self.view.backgroundColor = [UIColor whiteColor]; _mapView=[[MAMapView alloc]initWithFrame:self.view.bounds]; _mapView.delegate =self; _mapView.centerCoordinate = CLLocationCoordinate2DMake( _cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue); //_mapView.showsUserLocation = YES; //[_mapView setUserTrackingMode:MAUserTrackingModeFollow]; [self.view addSubview:_mapView]; MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init]; annotation.coordinate = CLLocationCoordinate2DMake(_cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue); //NSLog(@"%f---%f",_cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue); annotation.title = _cinemaModel.name; annotation.subtitle = _cinemaModel.addr; [_mapView addAnnotation:annotation]; } #pragma mark --MAMapViewDelegate代理---- -(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation { static NSString *annoId = @"PointAnnotation"; MAAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:annoId]; if (!view) { view = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId]; view.draggable = YES; view.canShowCallout = YES; view.image = [UIImage imageNamed:@"mark"]; } return view; }
三:自定制大头针
1.头文件
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> #import "BMapKit.h" //自定义大头针(必须具备系统大头针的属性) @interface MyAnontation : NSObject <BMKAnnotation> //大头针的编号 @property (nonatomic,assign) NSInteger anonIndex; //重新实现一个初始化方法 -(instancetype) initWithCoor:(CLLocationCoordinate2D ) coor title:(NSString *) title subTitle:(NSString *) subtitle; @end
2.对应的.m文件
#import "MyAnontation.h" @implementation MyAnontation { //经纬度 CLLocationCoordinate2D _coor; //主标题 NSString *_title; //副标题 NSString *_subTitle; } -(instancetype) initWithCoor:(CLLocationCoordinate2D)coor title:(NSString *)title subTitle:(NSString *)subtitle { self=[super init]; if (self) { _coor=coor; _title=title; _subTitle=subtitle; } return self; } #pragma mark-MKAnontation -(CLLocationCoordinate2D) coordinate { return _coor; } -(NSString *) title { return _title; } -(NSString *) subtitle { return _subTitle; } @end