iOS 大头针怎么固定在中间,且移动后及时更新位置信息

首先在viewdidload里增加地图的一个触摸事件
UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];  
    [self.mapView addGestureRecognizer:mTap];  

事件实现如下:
[cpp] view plaincopyprint?
- (void)tapPress:(UIGestureRecognizer*)gestureRecognizer {  
      
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];//这里touchPoint是点击的某点在地图控件中的位置  
    CLLocationCoordinate2D touchMapCoordinate =  
    [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了  
      

    NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);

}


// 以下是生成大头针的方法
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    NSString *AnnotationViewID = @"renameMark";
    if (newAnnotation == nil) {
        newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        
        // 设置颜色
        ((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;
        // 从天上掉下效果
        ((BMKPinAnnotationView*)newAnnotation).animatesDrop = YES;
        // 设置可拖拽
        ((BMKPinAnnotationView*)newAnnotation).draggable = YES;
        
       // newAnnotation.frame=CGRectMake(180, 200, 50, 50);
    }

    newAnnotation.centerOffset = CGPointMake(0, -(newAnnotation.frame.size.height * 0.5));
    newAnnotation.annotation = annotation;
    [newAnnotation setSelected:YES animated:YES];
    return newAnnotation;
}


MKMap显示地图后,如果用户移动了地图,自己定义的数据就需要刷新了,所以这个时候,中心点的经纬度就比较重要了。

本文演示如何获取经纬度

在MKMapViewDelegate里有个方法

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated  

这个方法就是在Map移动 后执行,所以我们可以在这里获取移动后地图中心点的经纬度了。

复制代码

 

 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    MKCoordinateRegion region;
    CLLocationCoordinate2D centerCoordinate = mapView.region.center;
    region.center= centerCoordinate;
    
    NSLog( @"  regionDidChangeAnimated %f,%f ",centerCoordinate.latitude, centerCoordinate.longitude);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值