百度地圖相關開發:顯示、定位、描點、自定義大頭針
阿新 • • 發佈:2018-12-21
一、環境配置 二、地圖顯示與定位 三、根據經緯度描點 四、大頭針與氣泡 五、給大頭針加tag值 六、問題處理
一、環境配置
1. 註冊和獲取金鑰:
2、下載.百度地圖framework包:
3、環境配置:
1>. 新增上述下載的包到專案;
2>. 新增系統framework包:
3>. 新增libstdc++.6.0.9.tbd:
自行從網上下載一個,拖進專案中。
4、設定-ObjC:
在TARGETS->Build Settings->Other Linker Flags 中新增-ObjC,字母O和C大寫。
執行沒有報錯, 環境配置成功!
二、地圖顯示與定位
1. 在AppDelegate 中註冊:
2. 地圖顯示:
3. 定位:
4. 協議返回地址資訊:
三、根據經緯度描點
實現程式碼:
1.初始化大頭針
_pointAnnotation = [[BMKPointAnnotation alloc] init];
2.將大頭針新增到地圖上
[_mapView addAnnotation:_pointAnnotation];
3. 給大頭針賦值
CLLocationCoordinate2D coor; coor.latitude = [latitudeStr doubleValue]; coor.longitude = [longitudeStr doubleValue]; _pointAnnotation.title = addrStr; _pointAnnotation.coordinate = coor;
4. 描點
[self.mapView setCenterCoordinate:coor animated:YES];
四、大頭針與氣泡
1.修改大頭針屬性
#pragma mark --- BMKMapViewDelegate - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{ //對大頭針以及氣泡進行一些設定 if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { static NSString *identifier = @"BMKAnnotationView"; BMKAnnotationView *annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; //修改大頭針圖片 annotationView.image = [UIImage imageNamed:@"location"]; //是否允許點選 annotationView.canShowCallout = YES; // 等一些其他屬性設定 ///..... return annotationView; } return nil; }
2. 點選大頭針
#pragma mark --- 點選大頭針事件
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{
//[mapView deselectAnnotation:view.annotation animated:NO];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"您點選了大頭針" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alert show];
}
3. 點選氣泡:
在此方法中可以獲取到點選了大頭針以及泡泡對應的資訊:
/// 要顯示的標題;注意:如果不設定title,無法點選annotation,也無法使用回撥函式;
@property (copy) NSString *title;
/// 要顯示的副標題
@property (copy) NSString *subtitle;
#pragma mark --- 點選氣泡事件
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"您點選了泡泡" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alert show];
}
但是如果對應一個介面返回的json資料,如果要回去點選了某條資料對應的id呢,該怎麼辦?怎麼給大頭針加唯一的標識?
百度地圖不支援直接新增tag值。 所以這裡自定義了一個大頭針賦值方法。
五、給大頭針加tag值
方法:
在- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation方法中替換掉原先的BMKAnnotationView;
#pragma mark --- BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
if ([annotation isKindOfClass:[self.pointAnnotation class]]) {
//陣列對應大頭針
paopaoIndex++;
UIView *paopaoView = [UIView new];
static NSString *identifier = @"XKAnnotationView";
//自定義的XKAnnotationView 替換掉原先的BMKAnnotationView
XKAnnotationView *annotationView = [[XKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier customView:paopaoView WithData: self.pointArray[paopaoIndex - 1] WithTag:1000 + paopaoIndex];
return annotationView;
}else{
//定位大頭針
}
return nil;
}
自定義AnnotationView
- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier customView:(UIView *)detailView WithData:(NSDictionary *)dataDict WithTag:(NSInteger)tag{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
if (self) {
[self setBounds:CGRectMake(0, 0, 100.f, 100.f)];
self.canShowCallout = YES;
self.centerOffset = CGPointMake(0, 0); //設定中心點偏移
detailView.tag = tag;
[self configCellWithData:dataDict];
[self addSubview: detailView];
}
}
return self;
}
六、問題處理
1、 +[BMKSearchReverseGeoCodeModel bmk_objectWithKeyValues:]: unrecognized selector sent to class 0x1068059a8
2、manager start failed : info.plist 中必須新增 Bundle display name
問題定位:info.plist中未新增:Bundle display name ,且其值不能為空。