iOS MapAnchor(地图锚点~自定义)~demo

//联系人:石虎  QQ: 1224614774昵称:嗡嘛呢叭咪哄

/**

注意点: 1.看 GIF 效果图.

       2.看连线视图的效果图.

       3.看实现代码(直接复制实现效果).

*/

一、GIF 效果图:




二、连线视图的效果图:

图1:


图2:



三、实现代码:

=========================

===================================================

==========================

/**

注意点: 1.自定义锚点定位图片(pos.gif )--->拷贝到桌面-->在拷贝到项目中 :

*/


//

//  ViewController.m

//  地图锚点(MapAnchor)

//

//  Created by 石虎 on 2017/7/5.

//  Copyright © 2017 shihu. All rights reserved.

//



#import "ViewController.h"

#import <MapKit/MapKit.h>//地图

#import <CoreLocation/CoreLocation.h>//定位


@interface ViewController () <MKMapViewDelegate>


//返回当前的位置

- (IBAction)goClicked:(id)sender;

//纬度

@property (strong,nonatomic)IBOutletUITextField *latitudeField;

//纬度

@property (strong,nonatomic)IBOutletUITextField *longitudeField;

//地图

@property (strong,nonatomic)IBOutletMKMapView *mapView;

//地理编码

@property (strong,nonatomic)CLGeocoder *geocoder;


@end


@implementation ViewController


- (void)viewDidLoad{

    [superviewDidLoad];

    //初始化地理编码

    _geocoder = [[CLGeocoderalloc]init];

    //设置地图的显示风格,此处设置使用标准地图

    self.mapView.mapType = MKMapTypeStandard;

    // 设置地图可缩放

    self.mapView.zoomEnabled =YES;

    // 设置地图可滚动

    self.mapView.scrollEnabled =YES;

    // 设置地图可旋转

    self.mapView.rotateEnabled =YES;

    //设置显示用户当前位置

    self.mapView.showsUserLocation = YES;

    //调用自己实现的方法设置地图的显示位置和显示区域

    [selflocateToLatitude:37.23longitude:122.1234];

    //创建一个手势处理器,用于检测、处理长按手势

    UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer

                                              alloc]initWithTarget:selfaction:@selector(longPress:)];

    //为该控件添加手势处理器

    [self.viewaddGestureRecognizer:gesture];

    

    //遵守代理是要实现自定义锚点

    self.mapView.delegate =self;

}


#pragma mark --手势回调

- (void) longPress:(UILongPressGestureRecognizer*)gesture{

    //获取长按点的坐标

    CGPoint pos = [gesturelocationInView:self.mapView];

    //将长按点的坐标转换为经度、维度值

    CLLocationCoordinate2D coord = [self.mapViewconvertPoint:pos

                                         toCoordinateFromView:self.mapView];

    // 将经度、维度值包装为CLLocation对象

    CLLocation* location = [[CLLocationalloc]initWithLatitude:coord.latitude

                                                      longitude:coord.longitude];

    //根据经、纬度反向解析地址

    [_geocoderreverseGeocodeLocation:locationcompletionHandler:

     ^(NSArray *placemarks,NSError *error)

     {

         if (placemarks.count >0 && error ==nil)

         {

             // 获取解析得到的第一个地址信息

             CLPlacemark* placemark = [placemarksobjectAtIndex:0];

             // 获取地址信息中的FormattedAddressLines对应的详细地址

             NSArray* addrArray = placemark

             .addressDictionary[@"FormattedAddressLines"];

             // 将详细地址拼接成一个字符串

             NSMutableString* address = [[NSMutableStringalloc]init];

             for(int i =0 ; i < addrArray.count ; i ++)

             {

                 [address appendString:addrArray[i]];

             }

             

             // 创建MKPointAnnotation对象——代表一个锚点

             MKPointAnnotation  *annotation = [[MKPointAnnotationalloc]init];

             

             annotation.title = placemark.name;

             annotation.subtitle = address;

             annotation.coordinate = coord;

             // 添加锚点

             [self.mapViewaddAnnotation:annotation];

         }

     }];

}


#pragma mark -点击回到输入的的经纬度位置

- (IBAction)goClicked:(id)sender{

    //关闭两个文本框的虚拟键盘

    [self.latitudeFieldresignFirstResponder];

    [self.longitudeFieldresignFirstResponder];

    //纬度

    NSString* latitudeStr =self.latitudeField.text;

    //经度

    NSString* longtitudeStr =self.longitudeField.text;

    //如果用户输入的经度、纬度不为空

    if (latitudeStr !=nil && latitudeStr.length >0

        && longtitudeStr != nil && longtitudeStr.length >0)

    {

        // 调用自己实现的方法设置地图的显示位置和显示区域

        [selflocateToLatitude:latitudeStr.floatValue

                     longitude:longtitudeStr.floatValue];

    }

}


#pragma mark --自定义封装定位方法

- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{

    //设置地图中心的经、纬度

    CLLocationCoordinate2D center = {latitude , longitude};

    //设置地图显示的范围,

    MKCoordinateSpan span;

    //地图显示范围越小,细节越清楚

    span.latitudeDelta =0.01;

    span.longitudeDelta =0.01;

    // 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。

    MKCoordinateRegion region = {center,span};

    //设置当前地图的显示中心和显示范围

    [self.mapViewsetRegion:regionanimated:YES];

    

    // 创建MKPointAnnotation对象——代表一个锚点

    MKPointAnnotation* annotation = [[MKPointAnnotationalloc]init];

    annotation.title =@"北京石羿科技发展有限公司";

    annotation.subtitle =@"海淀区中关村软件园";

    

    CLLocationCoordinate2D coordinate = {latitude , longitude};

    annotation.coordinate = coordinate;

    // 添加锚点

    [self.mapViewaddAnnotation:annotation];

}


#pragma mark -自定义锚点

// MKMapViewDelegate协议中的方法,该方法的返回值可用于定制锚点控件的外观

- (MKAnnotationView *) mapView:(MKMapView *)mapView

             viewForAnnotation:(id <MKAnnotation>) annotation{

    staticNSString* annoId =@"fkAnno";

    

    

    //获取可重用的锚点控件

    MKAnnotationView* annoView = [mapView

                                  dequeueReusableAnnotationViewWithIdentifier:annoId];

    //如果可重用的锚点控件不存在,创建新的可重用锚点控件

    if (!annoView)

    {

        annoView= [[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:annoId];

        /*

         如果不想改变锚点控件的图片,只想改变颜色,则可创建MKPinAnnotationView实例

         再修改MKPinAnnotationView对象的pinColor属性即可。

         */

    }

    //为锚点控件设置图片

    annoView.image = [UIImageimageNamed:@"pos.gif"];

    //设置该锚点控件是否可显示气泡信息

    annoView.canShowCallout =YES;

    //定义一个按钮,用于为锚点控件设置附加控件

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];

    //为按钮绑定事件处理方法

    [button addTarget:selfaction:@selector(buttonTapped:)

     forControlEvents:UIControlEventTouchUpInside];

    // 可通过锚点控件的rightCalloutAccessoryViewleftCalloutAccessoryView设置附加控件

    annoView.rightCalloutAccessoryView = button;

    return annoView;

    

}

#pragma mark -自定义锚点 --里面的详情按钮

- (void) buttonTapped:(id)sender

{

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

}


@end




谢谢!!!


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值