1.如何點擊按鈕放大和縮小地圖?
在地圖上創建兩個按鈕加上點擊事件,點擊事件中分別寫上如下方法即可實現:
創建按鈕:
//左邊按鈕
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftBtn.frame = CGRectMake(SCREEN_WIDTH*0.6, SCREEN_HEIGHT*0.93, 60, 30);
[leftBtn setBackgroundImage:[UIImage imageNamed:@"left_btn"] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftBtn];
//右邊按鈕
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
rightBtn.frame = CGRectMake(SCREEN_WIDTH*0.6+61, SCREEN_HEIGHT*0.93, 60, 30);
[rightBtn setBackgroundImage:[UIImage imageNamed:@"right_btn"] forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightBtn];
實現點擊事件:
- (void)leftBtnAction:(UIButton *)btn
{
[_mapView setZoomLevel:_mapView.zoomLevel-3];//縮小地圖
}
- (void)rightAction:(UIButton *)btn
{
[_mapView setZoomLevel:_mapView.zoomLevel+3];//放大地圖
}
2.當在地圖上大頭針要實現連續點擊事件上時該怎么做?(百度地圖大頭針默認只能點擊一次)
//實現連續點擊
[_mapView deselectAnnotation:view.annotation animated:YES];
3.當我們想要獲取手機屏幕上能看到的所有大頭針的經緯度時該怎么做?
//當前屏幕中心點的經緯度
CGFloat centerLongitude = self.mapView.region.center.longitude;
CGFloat centerLatitude = self.mapView.region.center.latitude;
//當前屏幕顯示范圍的經緯度
CLLocationDegrees pointssLongitudeDelta = self.mapView.region.span.longitudeDelta;
CLLocationDegrees pointssLatitudeDelta = self.mapView.region.span.latitudeDelta;
//左上角
CGFloat leftUpLong = centerLongitude - pointssLongitudeDelta/2.0;
CGFloat leftUpLati = centerLatitude - pointssLatitudeDelta/2.0;
//右上角
CGFloat rightUpLong = centerLongitude + pointssLongitudeDelta/2.0;
CGFloat rightUpLati = centerLatitude - pointssLatitudeDelta/2.0;
//左下角
CGFloat leftDownLong = centerLongitude - pointssLongitudeDelta/2.0;
CGFloat leftDownlati = centerLatitude + pointssLatitudeDelta/2.0;
//右下角
CGFloat rightDownLong = centerLongitude + pointssLongitudeDelta/2.0;
CGFloat rightDownLati = centerLatitude + pointssLatitudeDelta/2.0;
NSLog(@"\n 左上 %f,%f---------\n 右上 %f,%f-------\n 左下 %f,%f----- \n 右下 %f,%f",leftUpLong,leftUpLati,rightUpLong,rightUpLati,leftDownLong,leftDownlati,rightDownLong,rightDownLati);
4.百度地圖的一些基本設置:
//設置百度地圖的等級
[_mapView setZoomLevel:10];
//是否顯示比例尺
mapView.showMapScaleBar = YES;
//比例尺在地圖上的位置
mapView.mapScaleBarPosition = CGPointMake(10,mapView.frame.size.height-45);
//地圖是否支持旋轉,系統默認是旋轉的,即為YES,不想地圖旋轉設為NO
_mapView.rotateEnabled = YES;
//設定地圖是否現顯示3D樓塊效果
_mapView.buildingsEnabled = YES;
5.如何在地圖上循環創建多個大頭針?
//循環遍歷數組,數組里面包含所有的經緯度
for (AnnotaionModel *model in _annotaionArray) {
//判斷當經緯度為0的時候不在地圖上顯示
if ([model.Latitude doubleValue]==0||[model.Longitude doubleValue]==0) {
continue;
}
_annotion = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor = CLLocationCoordinate2DMake([model.Latitude doubleValue] , [model.Longitude doubleValue]);
_annotion.coordinate = coor;
[_mapView addAnnotation:_annotion];
}
6.當你不想用系統的大頭針,想用自己設置的圖片來顯示大頭針時怎么做?
#pragma mark --BMKMapViewDelegate百度地圖代理方法---
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id )annotation
{
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
for (AnnotaionModel *model in _annotaionArray) {
//設置大頭針圖片
newAnnotationView.image = [UIImage imageNamed:@"icon_stop"];
//當設為YES時view被選中時會彈出氣泡,annotation必須實現了title這個方法,當為NO時點擊大頭針不會彈出氣泡
newAnnotationView.canShowCallout = NO;
return newAnnotationView;
}
7.如何獲取自身定位的經緯度:
#pragma mark --BMKLocationServiceDelegate百度地圖定位代理方法--
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
NSLog(@"%f,%f",coor.latitude,coor.longitude);
}
8.如何實現定位?
//自iOS SDK v2.5.0起,為了對iOS8的定位能力做兼容,做了相應的修改,開發者在使用過程中注意事項如下: 需要在info.plist里添加(以下二選一,兩個都添加默認使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允許在前臺使用時獲取GPS的描述
NSLocationAlwaysUsageDescription ,允許永久使用GPS的描述
_locationService = [[BMKLocationService alloc]init];
_locationService.delegate =self;//設置代理
[_locationService startUserLocationService];
//實現定位的代理方法
#pragma mark ----BMKLocationServiceDelegate---
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
_mapView.centerCoordinate = coor;
[_mapView updateLocationData:userLocation];
}
最后打個小廣告不是介意哈! iOS開發交流群:529052159,iOS公眾號:iOS開發精髓,每天都會發布精美文章。動動你的小手掃碼關注吧!
Paste_Image.png