最近公司在做地图用的是百度地图,百度文档的坑不用说了 我们的功能不算复杂 在这里说下
1.首先:老一套 各种配置 网上有好多 我也是在网上找的
2.我用的是3.3.4版本的地图,所以有好多东西都和以前的一样
创建地图
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, self.view.frame.size.height - 120)];
[self.view addSubview:_mapView];
各种设置,详细的可以去找那些大神的文章看看 我这里就是简单的使用
_mapView.zoomLevel = 20;
_mapView.rotateEnabled = NO;//这个是设置地图可不可以旋转的
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = BMKUserTrackingModeNone;
肯定少不了定位的啦
_locService = [[BMKLocationService alloc]init];
_locService.distanceFilter = 10;
//启动定位服务
[_locService startUserLocationService];
定位的代理方法
#pragma mark 处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation*)userLocation{
_locationString= [NSStringstringWithFormat:@"%f|%f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude];
[self.locService stopUserLocationService];
}
大头针的设置
- (BMKAnnotationView*)mapView:(BMKMapView*)mapView viewForAnnotation:(id)annotation{
NSString*AnnotationViewID =@"AnnotationViewID";
_NewAnnotation= [[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:AnnotationViewID];
// 设置颜色
((BMKPinAnnotationView*)_NewAnnotation).pinColor = BMKPinAnnotationColorPurple;
// 从天上掉下效果
((BMKPinAnnotationView*)_NewAnnotation).animatesDrop = YES;
// 设置可拖拽
((BMKPinAnnotationView*)_NewAnnotation).draggable = YES;
//设置大头针图标
((BMKPinAnnotationView*)_NewAnnotation).image= [UIImageimageNamed:[NSStringstringWithFormat:@"location_y%d", i]];
return _NewAnnotation;
}
//这个是点击大头针的方法,不过这里要提醒一下 要设置了大头针的标题或者副标题才能点击
- (void)mapView:(BMKMapView*)mapView didSelectAnnotationView:(BMKAnnotationView*)view{
}
*********** 最重要的就是遵循百度的代理 要卸载下面的这两个方法里面 ***************
#pragma mark 遵循代理写在viewwillappear中
- (void)viewWillAppear:(BOOL)animated {
[_mapView viewWillAppear];
_mapView.delegate = self;
_locService.delegate = self;
_geoCodeSearch.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated {
[_mapView viewWillDisappear];
_mapView.delegate = nil;
_locService.delegate = nil;
_geoCodeSearch.delegate = nil;
}
还有就是头文件 代理之类的了
#import "BaiduMapAPI_Base/BMKBaseComponent.h"
#import "BaiduMapAPI_Map/BMKMapComponent.h"
#import "BaiduMapAPI_Search/BMKSearchComponent.h"
#import "BaiduMapAPI_Cloud/BMKCloudSearchComponent.h"
#import "BaiduMapAPI_Location/BMKLocationComponent.h"
#import "BaiduMapAPI_Utils/BMKUtilsComponent.h"
#import "BaiduMapAPI_Radar/BMKRadarComponent.h"
#import "BaiduMapAPI_Map/BMKMapView.h"
遵循代理
<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,BMKGeneralDelegate>
我在这里都写了一个全局
//地图相关
@property(nonatomic,strong)BMKMapView* mapView;
@property(nonatomic,strong)BMKLocationService* locService;
@property(nonatomic,strong)BMKGeoCodeSearch* geoCodeSearch;
@property(nonatomic,strong)BMKPointAnnotation* pointAnnotation;
@property(nonatomic,strong)BMKPinAnnotationView * NewAnnotation;
还有一个就是去掉百度地图的Logo也是在网上找的
//去掉百度地图的Logo
for(UIView*viewinself.mapView.subviews) {
for(UIImageView*imageViewinview.subviews) {
staticinta =0;
a ++;
if(a ==4) {
[imageViewremoveFromSuperview];
}
}
}
今天的新需求 要根据后台返回的地理坐标 在地图上画出来 是一个 数组里面有好多坐标,我们这里后台是吧所有的坐标点 放在了字符串里面 ()
NSString * points = @"116.38129,39.88384;116.381299,39.882387;116.383239,39.882342;116.383226,39.883889";
NSMutableArray * arrayM = [NSMutableArray array];
NSArray *array = [points componentsSeparatedByString:@";"];
for(int i =0; I<arrayM.coun;i++){
[arrayMaddObject:array[i]];
}
NSMutableArray * arrayM = [NSMutableArray array];
NSArray *array = [points componentsSeparatedByString:@";"];
for(int i =0; I <array.count;i++){
[arrayMaddObject:array[i]];
}
NSMutableArray * dictarray = [NSMutableArray array];
for(NSString* CoordinateStringinarrayM) {
NSArray*array = [CoordinateStringcomponentsSeparatedByString:@","];
NSString*longitudeString = array[0];
NSString*latitudeeString = array[1];
NSDictionary* CoordinateDict=@{@"longitude":longitudeString,@"latitude":latitudeeString};
[dictarrayaddObject:CoordinateDict];
}
NSIntegersize = dictarray.count;
CLLocationCoordinate2D * coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D)*size);
for(int k =0; k <array.count;k++){
NSDictionary* dict = dictarray[k];
coords[k].latitude=[dict[@"latitude"]floatValue];
coords[k].longitude= [dict[@"longitude"]floatValue];
}
BMKPolygon* polygon = [BMKPolygon polygonWithCoordinates:coords count:size];
[_mapViewaddOverlay:polygon];
最后那个是 我们返回的坐标字符串 根据你们的数据进行处理就可以了
// 一定要写这个 这个是给你画的线 处理颜色 以及 覆盖物 上颜色的方法
- (BMKOverlayView*)mapView:(BMKMapView*)mapView viewForOverlay:(id)overlay{
if([overlayisKindOfClass:[BMKPolygonclass]]){
BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay] ;
polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
polygonView.lineWidth=2.0;
returnpolygonView;
}
return nil;
}
//根据返回的坐标点画线
NSIntegersize = dataArray.count;
CLLocationCoordinate2D * coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D)*size);
for(intk =0; k
NSDictionary* dict = dataArray[k];
coords[k].longitude= [dict[@"xaxis"]floatValue];
coords[k].latitude= [dict[@"yaxis"]floatValue];
}
BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coords count:size];
[_mapView addOverlay:polyline];
就这些 了 希望能帮到 需要帮助的人