地图不能拖动问题
如果不能销毁的界面,离开界面执行[mapView viewWillDisappear]后,不要在执行操作地图的代码(举个例子,比如有个网络请求之后要对地图操作的代码,因为你请求发出之后可能还没有返回,你却离开了当前界面(界面没销毁),但之后请求返回了,就去执行对地图操作的代码,我想此时可能就把开始的那个地图给唤醒了,那么下个界面的地图就不能拖动了,只有回到原来的界面之后才能拖动)。解决方法:可以设置一个bool类型的属性,视图也就是界面将要显示的时候设置为YES,将要离开的时候设置为NO,如果有需要请求或怎样之后要操作地图的代码,可以加一个判断,为YES的时候执行,为NO的时候不执行。
地图自身相关
1.地图不显示
导入mapapi.budle
2.设定地图中心点
//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
//NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
//NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
[[NSNotificationCenter defaultCenter] postNotificationName:MY_LOCATION_UPDATE_NOTIFICATION object:nil userInfo:@{@"loc" : userLocation}];
}
//更新我的位置
- (void)updateMyLocation:(NSNotification *) notification {
BMKUserLocation *location = [notification.userInfo objectForKey:@"loc"];
_myCoor = location.location.coordinate;
//NSLog(@"%@",location);
/**
*设定地图中心点坐标
*@param coordinate 要设定的地图中心点坐标,用经纬度表示
*@param animated 是否采用动画效果
*/
[_mapView setCenterCoordinate:_myCoor animated:YES];
[_mapView updateLocationData:location];
}
3.添加定位到当前位置和放大缩小比例尺按钮
@property (nonatomic,strong)UIButton *currentLocationBtn;//当前位置
@property (nonatomic,strong)UIButton *zoomInBtn; //放大
@property (nonatomic,strong)UIButton *zoomOutBtn;//缩小
-(void)viewDidload
{
...............
[_mapView addSubview:self.currentLocationBtn];
[_mapView addSubview:self.zoomInBtn];
[_mapView addSubview:self.zoomOutBtn];
}
//定位
- (UIButton *)currentLocationBtn {
if (!_currentLocationBtn) {
_currentLocationBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, ScreenHeight*0.6, 50, 50)];
[_currentLocationBtn setImage:[UIImage imageNamed:@"currentLocationBtn"] forState:UIControlStateNormal];
[_currentLocationBtn addTarget:self action:@selector(clickCurrentBtn) forControlEvents:UIControlEventTouchUpInside];
}
return _currentLocationBtn;
}
//点击定位
- (void)clickCurrentBtn {
[_mapView setCenterCoordinate:_myCoor animated:YES];
}
//放大
- (UIButton *)zoomInBtn {
if (!_zoomInBtn) {
_zoomInBtn = [[UIButton alloc] initWithFrame:CGRectMake(KscreenW-60, ScreenHeight*0.55, 50, 50)];
[_zoomInBtn setImage:[UIImage imageNamed:@"zoomin"] forState:UIControlStateNormal];
[_zoomInBtn addTarget:self action:@selector(mapZoomIn) forControlEvents:UIControlEventTouchUpInside];
}
return _zoomInBtn;
}
//放大方法
- (void)mapZoomIn {
_mapView.zoomLevel = _mapView.zoomLevel+1;
// MKCoordinateRegion region = self.map.region;
// region.span.latitudeDelta = region.span.latitudeDelta * 0.5;
// region.span.longitudeDelta = region.span.longitudeDelta * 0.5;
// [self.map setRegion:region animated:YES];
}
//缩小
- (UIButton *)zoomOutBtn {
if (!_zoomOutBtn) {
_zoomOutBtn = [[UIButton alloc] initWithFrame:CGRectMake(KscreenW-60, CGRectGetMaxY(self.zoomInBtn.frame), 50, 50)];
[_zoomOutBtn setImage:[UIImage imageNamed:@"zoomout"] forState:UIControlStateNormal];
[_zoomOutBtn addTarget:self action:@selector(mapZoomOut) forControlEvents:UIControlEventTouchUpInside];
}
return _zoomOutBtn;
}
//缩小方法
- (void)mapZoomOut {
// MKCoordinateRegion region = self.map.region;
// region.span.latitudeDelta = region.span.latitudeDelta * 2;
// region.span.longitudeDelta = region.span.longitudeDelta * 2;
// [self.map setRegion:region animated:YES];
_mapView.zoomLevel = _mapView.zoomLevel-1;
}
标注相关(大头针)
**添加不同标注**
定义全局变量
BMKPointAnnotation* animatedAnnotation1;
BMKPointAnnotation* animatedAnnotation2;
// 添加动画Annotation
- (void)addAnimatedAnnotation
{
animatedAnnotation = [[BMKPointAnnotation alloc]init];
//CLLocationCoordinate2D coor;
//coor.latitude = 34.799276;
//coor.longitude = 113.605937;
animatedAnnotation.coordinate = _myCoor;
animatedAnnotation.title = [NSString stringWithFormat:@"有%lu位大神等待接单",(unsigned long)fjUserArr.count];;
[_mapView addAnnotation:animatedAnnotation];
}
#pragma mark 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation
{
if (annotation == animatedAnnotation) {
NSString *AnnotationViewID = @"AnimatedAnnotation";
MyAnimatedAnnotationView *annotationView = nil;
if (annotationView == nil) {
annotationView = [[MyAnimatedAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
NSMutableArray *images = [NSMutableArray array];
for (int i = 1; i < 4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"poi_%d.png", i]];
[images addObject:image];
}
annotationView.annotationImages = images;
return annotationView;
}
}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation
该类类似于tableview的 cellforrow方法,会执行多次
可以使用继承
@interface GPLBMKPinAnnotationView : BMKPinAnnotationView
@property(nonatomic,strong)NSString *nameStr;//名字
@property(nonatomic,strong)NSString *useridStr;//商户id
@property(nonatomic,strong)NSString *fuwuidStr;//服务id
@end
NSString *AnnotationViewID = [NSString stringWithFormat:@"renameMark%d",iii];
GPLBMKPinAnnotationView *newAnnotation = [[GPLBMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
// 设置颜色
((GPLBMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;
// 从天上掉下效果
((GPLBMKPinAnnotationView*)newAnnotation).animatesDrop = YES;
// 设置可拖拽
((GPLBMKPinAnnotationView*)newAnnotation).draggable = YES;
iii++;
//当选中一个annotation views时,调用此接口
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
if ([view isKindOfClass:[GPLBMKPinAnnotationView class]]) {
GPLBMKPinAnnotationView *newAnnotation = (BMKAnnotationView *)view;
NSLog(@"11111111111111111111111");
ShanghuziliaoViewController *shoperDetailVC = [[ShanghuziliaoViewController alloc]init];
shoperDetailVC.sellerid = newAnnotation.useridStr;
shoperDetailVC.tittleStr = newAnnotation.nameStr;
shoperDetailVC.sellerfuwuid = newAnnotation.fuwuidStr;
NSString *str1 = @"1";
shoperDetailVC.isSeller = 1;
shoperDetailVC.stateStrs = str1;
[self.navigationController pushViewController:shoperDetailVC animated:YES];
} else {
NSLog(@"点击自身的大头针");
// MyListViewController *myListVC = [[MyListViewController alloc]init];
// [self.navigationController pushViewController:myListVC animated:YES];
}
}