ios地图锚点

1.话不多说,效果

ios地图锚点_第1张图片
Untitled2.gif

2.注意点

跟大家多唠叨一下,设置锚点的意义就是可以进行详情展示,大家可以设置锚点的样式。效果中的样式是一张图片,没有图片显示的就是我们俗称的大头针。
锚点.gif

3.代码展示

//地理编码
@property(nonatomic,strong)CLGeocoder *geocoder;
self.geocoder = [[CLGeocoder alloc]init];
    
//viewDidLoad
    //设置地图的显示风格
    self.mapView.mapType = MKMapTypeStandard;
    //设置地图可缩放
    self.mapView.zoomEnabled = YES;
    //设置地图可滚动
    self.mapView.scrollEnabled = YES;
    //设置地图可旋转
    self.mapView.rotateEnabled = YES;
    //设置显示用户当前位置
    self.mapView.showsUserLocation = YES;
    //为mapView设置代理
    self.mapView.delegate = self;
    
    [self locateTolatitude:23.126272 longitude:113.395568];
    
    //创建一个手势处理器,用于检测,处理长按手势
    //长按手势识别器
    UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    
    //为该控件添加手势
    [self.view addGestureRecognizer:longPressGes];
#pragma mark - 手势回调
-(void)longPress:(UILongPressGestureRecognizer *)gestu
{
    //获取长按点的坐标
    CGPoint pos = [gestu locationInView:self.mapView];
    
    //把获取到的坐标转换成经度纬度 值
    CLLocationCoordinate2D coor2D = [self.mapView convertPoint:pos toCoordinateFromView:self.mapView];
    //再把经纬度值添加到CLLocation(定位)对象
    CLLocation *location = [[CLLocation alloc]initWithLatitude:coor2D.latitude longitude:coor2D.longitude];
    
    //根据经纬度解析地址(反向解析)
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
       
        if (placemarks.count > 0) {
            CLPlacemark *placmark = placemarks[0];
            
            NSArray *addrArray = [placmark.addressDictionary objectForKey:@"FormattedAddressLines"];
            //将详细地址拼接成一个字符串
            NSMutableString *address = [[NSMutableString alloc]init];
            for (int i = 0 ; i < addrArray.count ; i++) {
                [address appendString:addrArray[i]];
            }
            //创建MKPointAnnotation对象---代表一个锚点
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
            //设置标题
            annotation.title = placmark.name;
            //设置子标题
            annotation.subtitle = address;
            //把坐标放入锚点里面
            annotation.coordinate = coor2D;
            //添加锚点
            [self.mapView addAnnotation:annotation];
        }
        
    }];
   
    
}
- (IBAction)gogogo:(id)sender {
    //关闭两个文本框的虚拟键盘
    [self.weidu resignFirstResponder];
    [self.jingdu resignFirstResponder];
    NSString* latitudeStr = self.weidu.text;
    NSString* longtitudeStr = self.jingdu.text;
    //如果用户输入的经纬度不为空
    if (latitudeStr != nil && latitudeStr.length > 0
        && longtitudeStr != nil && longtitudeStr.length > 0)
    {
        // 调用自己实现的方法设置地图的显示位置和显示区域
        [self locateTolatitude:latitudeStr.floatValue
                     longitude:longtitudeStr.floatValue];
    }
}
-(void)locateTolatitude:(CGFloat)latitude longitude:(CGFloat)longitude
{
    CLLocationCoordinate2D coor2D = {latitude,longitude};
    
        //设置地图的显示范围
        MKCoordinateSpan span;
        //地图显示范围越小,越清楚
        span.latitudeDelta = 0.01;
        span.longitudeDelta = 0.01;
        // 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。
        MKCoordinateRegion region = {coor2D,span};
        // 设置当前地图的显示中心和显示范围
        [self.mapView setRegion:region animated:YES];
    //将用户输入的经纬度封装成CLLocation对象
    CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
        
        //如果解析结果的集合元素大于1,表明解析得到了经度,维度信息
        if (placemarks.count > 0) {
            CLPlacemark *placmark = placemarks[0];
            NSLog(@"===%@",placmark.addressDictionary);
            //获取详细地址信息
            NSArray *addArr = [placmark.addressDictionary objectForKey:@"FormattedAddressLines"];
            //将详细地址拼接成一个字符串
            NSMutableString *addr = [[NSMutableString alloc]init];
            for (int i = 0; i < addArr.count; i ++) {
                [addr appendString:addArr[i]];
            }
            //创建MKPointAnnotation对象---代表一个锚点
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
            //设置标题
            annotation.title = placmark.name;
            //设置子标题
            annotation.subtitle = addr;
            //把坐标放入锚点里面
            annotation.coordinate = coor2D;
            //添加锚点
            [self.mapView addAnnotation:annotation];

            
        }
        
    }];

    
    
}

#pragma mark -MKMapViewDelegate(地图代理)
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
    static NSString *annoId = @"fkAnno";
    //获取可重用的锚点控件
    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoId];
    if (!annoView) {
        
        annoView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoId];
    }
    /*
     如果不想改变锚点控件的图片,只想改变颜色,则可创建MKPinAnnotationView实例
     再修改MKPinAnnotationView对象的pinColor属性即可。
     */
    annoView.image = [UIImage imageNamed:@"锚点.gif"];
     // 设置该锚点控件是否可显示气泡信息
    annoView.canShowCallout = YES;
     // 定义一个按钮,用于为锚点控件设置附加控件
    //UIButtonTypeDetailDisclosure 详细说明蓝色小箭头按钮
    UIButton *b = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [b addTarget:self action:@selector(butomTape:) forControlEvents:UIControlEventTouchUpInside];
     // 可通过锚点控件的rightCalloutAccessoryView、leftCalloutAccessoryView设置附加控件
    annoView.rightCalloutAccessoryView = b;
    
    return annoView;
}
-(void)butomTape:(UIButton *)sender

{
    NSLog(@"您点击了锚点信息!");
}

你可能感兴趣的:(ios地图锚点)