地图锚点的使用方法

2017-08-03  本文已影响0人  J_mine

需在Connection中选中Action锚点针对于地图定位功能,就是我们日常生活中地图定位的小图标,为我们的出行提供和了极大方便,现在就为大家介绍一下在ios开发中锚点的使用方法与步骤(此代码使用的是固定的位置,可针对其需要加以具体的修改)

一、 在ViewController.m中导入我们所需要的头文件

#import <MapKit/MapKit.h>//地图
#import <CoreLocation/CoreLocation.h>//定位

二、 对页面进行布局,拖拽控件并将我们所要用到的控件与ViewController进行关联 具体如下图所示

屏幕快照 2017-08-03 下午7.09.19.png

注意: 在进行控件的拖拽时 我们要掌握先后的次序,先拖拽Map View 控件,并将它不满全屏,其次在将 lable Text Field Button 放在Map View 上,在关联button时需在Connection中选中Action再进行关联

具体关联后的效果 如下图所示

屏幕快照 2017-08-03 下午7.14.39.png

三、因为我们要实现的功能有手势的需要 所以我们要为程序 写一下手势的代码并将地图的一个必要功能实现在.m中

- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化地理编码
    _geocoder = [[CLGeocoder alloc] init];
    // 设置地图的显示风格,此处设置使用标准地图
    self.mapView.mapType = MKMapTypeStandard;
    // 设置地图可缩放
    self.mapView.zoomEnabled = YES;
    // 设置地图可滚动
    self.mapView.scrollEnabled = YES;
    // 设置地图可旋转
    self.mapView.rotateEnabled = YES;
    // 设置显示用户当前位置
    self.mapView.showsUserLocation = YES;
    // 调用自己实现的方法设置地图的显示位置和显示区域
     [self locateToLatitude:37.23 longitude:122.1234];
    
   // 创建一个手势处理器 用于检测处理 长按手势
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
   // 为该控件 添加手势处理器
    [self.view addGestureRecognizer:gesture];
   // 设置代理方法
    self.mapView.delegate = self;
    
}

四 、接下来我们要对手势的回调写具体的代码

#pragma mark -手势回调
-(void)longPress:(UILongPressGestureRecognizer *)getsure
{
    // 获取长按点的坐标
    CGPoint pos = [getsure locationInView:self.mapView];
    
    // 把获取到的坐标转换成经纬度
    CLLocationCoordinate2D coord = [self.mapView convertPoint:pos toCoordinateFromView:self.mapView];
    // 再把经纬度值添加到  CLLocation(定位)
    CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
    // 根据经纬度反向解析地址
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
    {
        if (placemarks.count >0 &&  error == nil)
        {
            // 获取解析得到的第一个地址信息
            CLPlacemark *placemark = placemarks[0];
            
            // 获取地址信息中的FormattedAddressLines对应的详细地址
            NSArray *addrArray = placemark.addressDictionary[@"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 = placemark.name;
            // 设置子标题
            annotation.subtitle = address;
            // 把坐标放入锚点里面
            annotation.coordinate = coord;
            // 添加锚点
            [self.mapView addAnnotation:annotation];
        }
       
        
    }];
}


五、点击回到输入的经纬度位置

- (IBAction)goClicked:(id)sender
{
    // 关闭两个文本框的虚拟键盘
    [self.latitudeField resignFirstResponder];
    [self.longitudeField resignFirstResponder];
    //纬度
    NSString* latitudeStr = self.latitudeField.text;
    //经度
    NSString* longtitudeStr = self.longitudeField.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 center = {latitude , longitude};
    // 设置地图显示的范围,
    MKCoordinateSpan span;
    // 地图显示范围越小,细节越清楚
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    // 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。
    MKCoordinateRegion region = {center,span};
    // 设置当前地图的显示中心和显示范围
    [self.mapView setRegion:region animated:YES];
    
    // 创建MKPointAnnotation对象——代表一个锚点
    MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
    annotation.title = @"北京石羿科技发展有限公司";
    annotation.subtitle = @"海淀区中关村软件园";
    
    CLLocationCoordinate2D coordinate = {latitude , longitude};
    annotation.coordinate = coordinate;
    // 添加锚点
    [self.mapView addAnnotation:annotation];
}

七、对锚点的外观进行具体的设置

// MKMapViewDelegate协议中的方法,该方法的返回值可用于定制锚点控件的外观
- (MKAnnotationView *) mapView:(MKMapView *)mapView
             viewForAnnotation:(id <MKAnnotation>) 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:@"pos.gif"];
    // 设置该锚点控件是否可显示气泡信息
    annoView.canShowCallout = YES;
    // 定义一个按钮,用于为锚点控件设置附加控件
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    // 为按钮绑定事件处理方法
    [button addTarget:self action:@selector(buttonTapped:)
     forControlEvents:UIControlEventTouchUpInside];
    // 可通过锚点控件的rightCalloutAccessoryView、leftCalloutAccessoryView设置附加控件
    annoView.rightCalloutAccessoryView = button;
    return annoView;
    
}

- (void) buttonTapped:(id)sender
{
    NSLog(@"您点击了锚点信息!");
}

最后可在打印栏中看到以上代码的信息

上一篇 下一篇

猜你喜欢

热点阅读