如果要用百度地图,倒入SDK是关键,首先在百度地图SDK官网,下载百度地图SDK,把 inc 文件夹 和Mapapi.bundle 图片库导入工程,其次把 libbaidumapapi.a 文件导入工程,导入静态库:
SystemConfiguration.framework
OpenGLES.framework
CoreLocation.framework
QuartzCore.framework
在BuildSettings 里的linking 设置OtherOtherLinkerFlag 的值为 -all_load 如图:
seatchPaths 里的LibrarySearchPaths 的三个值全部删除,只设为$(SRCROOT)/libs/Release$(EFFECTIVE_PLATFORM_NAME) "$(SRCROOT)" 足以。
把Appdelegate.m改为 .mm ,
把你定义显示地图的.m文件也改为.mm.
在Appdelegate.h 中包含头文件
#import "BMapKit.h"
声明代理:
<BMKGeneralDelegate>
在Appdelegate.mm 里声明:
BMKMapManager* _mapManager;
_mapManager = [[BMKMapManageralloc]init];
BOOL ret = [_mapManagerstart:@"你申请的appkey"generalDelegate:self];
if (!ret) {
NSLog(@"manager start failed!");
}
下面就是显示地图了:_mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0,0,320,480)];
_mapView.delegate =self;
self.view =_mapView;
//地图样式
_mapView.mapType =BMKMapTypeTrafficOn; //交通路况图
//定位
_mapView.showsUserLocation =YES;
其中定位代理方法://定位
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
{
if (userLocation != nil) {
NSLog(@"%f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
}
CLLocationCoordinate2D coor;
coor = userLocation.location.coordinate;
//coor.longitude = 116.404;
// mapView.center = Cgrect;
//地图中心点
// [mapView setCenterCoordinate:coor];
}
添加大头针:BMKPointAnnotation* annotation = [[BMKPointAnnotationalloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 39.111;
coor.longitude = 117.261;
annotation.coordinate = coor;
annotation.title = @"test";
annotation.subtitle =@"此Annotation可拖拽!";
[_mapView addAnnotation:annotation];
//定义手势长按添加大头针
UILongPressGestureRecognizer *lpress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];
lpress.minimumPressDuration =0.5;//按0.5秒响应longPress方法
lpress.allowableMovement =10.0;
[_mapViewaddGestureRecognizer:lpress];
其中手势调用的方法:
//长按屏幕添加大头针
- (void)longPress:(UIGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state ==UIGestureRecognizerStateEnded){
return;
}
//坐标转换
CGPoint touchPoint = [gestureRecognizerlocationInView:_mapView];
CLLocationCoordinate2D touchMapCoordinate =
[_mapView convertPoint:touchPointtoCoordinateFromView:_mapView];
BMKPointAnnotation* pointAnnotation = nil;
pointAnnotation = [[BMKPointAnnotation alloc] init];
pointAnnotation.coordinate = touchMapCoordinate;
pointAnnotation.title = @"名字";
[_mapView addAnnotation:pointAnnotation];
}
大头针的代理方法:
//大头针可拖动
// Override
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];
newAnnotation.pinColor =BMKPinAnnotationColorPurple;
newAnnotation.animatesDrop =YES;
newAnnotation.draggable =YES;
return newAnnotation;
}
return nil;
}
//点击气泡调用方法
// Override
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;
{
NSLog(@"paopaoclick");
}
// 添加圆形覆盖物
CLLocationCoordinate2D coor2;
coor2.latitude = 39.915;
coor2.longitude = 116.404;
BMKCircle* circle = [BMKCirclecircleWithCenterCoordinate:coor2radius:5000];
[_mapView addOverlay:circle];
// 添加多边形覆盖物
CLLocationCoordinate2D coords[4] = {0};
coords[0].latitude =39.915;
coords[0].longitude =116.404;
coords[1].latitude =39.815;
coords[1].longitude =116.404;
coords[2].latitude =39.815;
coords[2].longitude =116.504;
coords[3].latitude =39.915;
coords[3].longitude =116.504;
BMKPolygon* polygon = [BMKPolygonpolygonWithCoordinates:coordscount:4];
[_mapView addOverlay:polygon];
//添加折线覆盖物
CLLocationCoordinate2D coors[2] = {0};
coors[0].latitude =39.895;
coors[0].longitude =116.354;
coors[1].latitude =39.815;
coors[1].longitude =116.304;
BMKPolyline* polyline = [BMKPolylinepolylineWithCoordinates:coorscount:2];
[_mapView addOverlay:polyline];
//比例尺级别
[_mapView setZoomLevel:11];
其中添加自定义图层代理方法:
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKCircle class]])
{
BMKCircleView* circleView = [[BMKCircleViewalloc]initWithOverlay:overlay];
circleView.fillColor = [[UIColorredColor]colorWithAlphaComponent:0.5];
circleView.strokeColor = [[UIColorblueColor]colorWithAlphaComponent:0.5];
circleView.lineWidth = 5.0;
return circleView;
}
if ([overlay isKindOfClass:[BMKPolylineclass]])
{
BMKPolylineView* polylineView = [[BMKPolylineViewalloc]initWithOverlay:overlay];
polylineView.strokeColor = [[UIColorblueColor]colorWithAlphaComponent:1];
polylineView.lineWidth = 3.0;
return polylineView;
}
if ([overlay isKindOfClass:[BMKPolygon class]])
{
BMKPolygonView* polygonView = [[BMKPolygonViewalloc]initWithOverlay:overlay];
polygonView.strokeColor = [[UIColorpurpleColor]colorWithAlphaComponent:1];
polygonView.fillColor = [[UIColorcyanColor]colorWithAlphaComponent:0.2];
polygonView.lineWidth =2.0;
return polygonView;
}
return nil;
}