• 添加大头针


    大头针的基本操作
    添加一个大头针
    - (void)addAnnotation:(id <MKAnnotation>)annotation;
    
    添加多个大头针
    - (void)addAnnotations:(NSArray *)annotations;
    
    移除一个大头针
    - (void)removeAnnotation:(id <MKAnnotation>)annotation;
    
    移除多个大头针
    - (void)removeAnnotations:(NSArray *)annotations;
    
    (id <MKAnnotation>)annotation参数是什么东西?
    大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据

    自定义大头针,需要实现MKAnnotation协议

    @interface MyAnnotation : NSObject <MKAnnotation>
    
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    
    @end

    控制器:

    //
    //  ViewController.m
    //  05-添加大头针
    //
    //  Created by apple on 15/1/30.
    //  Copyright (c) 2015年 apple. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <MapKit/MapKit.h>
    #import "MyAnnotation.h"
    
    @interface ViewController () <MKMapViewDelegate>
    
    // 显示地图的View
    @property (weak, nonatomic) IBOutlet MKMapView *mapView;
    
    // IOS8
    @property (nonatomic,strong) CLLocationManager *mgr;
    
    @end
    
    @implementation ViewController
    
    - (CLLocationManager *)mgr{
        if (!_mgr) {
            _mgr = [[CLLocationManager alloc] init];
            
            [_mgr requestAlwaysAuthorization];
            [_mgr requestWhenInUseAuthorization];
        }
        return _mgr;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // iOS 8后需要请求授权,并在info.plist文件中添加
    //        总是使用用户位置:NSLocationAlwaysUsageDescription
    //        使用应用时定位:NSLocationWhenInUseDescription
        self.mgr;
        
        // 1.设置代理
        self.mapView.delegate = self;
        
        // 2.跟踪用户的位置
        self.mapView.userTrackingMode = MKUserTrackingModeFollow;
        
        // 3.添加两个大头针
        MyAnnotation *anno1 = [[MyAnnotation alloc] init];
        anno1.coordinate = CLLocationCoordinate2DMake(40.06, 116.39);
        anno1.title = @"北京市";
        anno1.subtitle = @"中国北京市昌平区";
        
        MyAnnotation *anno2 = [[MyAnnotation alloc] init];
        anno2.coordinate = CLLocationCoordinate2DMake(30.23, 120.23);
        anno2.title = @"杭州市";
        anno2.subtitle = @"浙江省杭州市萧山区";
        
        [self.mapView addAnnotation:anno1];
        [self.mapView addAnnotation:anno2];
    }
    
    /**
     *  定位到用户的位置会调用该方法
     *
     *  @param userLocation 大头针模型对象
     */
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        // 设置用户的位置为地图的中心点
        [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    }
    
    /**
     *  点击屏幕任意位置添加打头针
     *
     *  @param touches <#touches description#>
     *  @param event   <#event description#>
     */
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        // 1.获取用户点击的点
        CGPoint point = [[touches anyObject] locationInView:self.view];
        
        // 2.将该点转化成经纬度(地图上的坐标)
        CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.view];
        
        // 3.添加大头针
        MyAnnotation *anno = [[MyAnnotation alloc] init];
        anno.coordinate = coordinate;
        anno.title = @"大头针";
        anno.subtitle = @"大头针大头针";
        [self.mapView addAnnotation:anno];
    }
    
    @end
    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    
    @interface MyAnnotation : NSObject <MKAnnotation>
    
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    
    @end
  • 相关阅读:
    [C++]DirectShow检测音视频输入设备及其采集参数
    [C#] 使用Accord.Net,实现相机画面采集,视频保存及裁剪视频区域,利用WriteableBitmap高效渲染
    [C#]使用第三方开源库iText7.pdfHtml,将Html转换成Pdf,以及如何以Html作为打印模板
    C# 佳能相机SDK对接,采集并保存视频,使用WriteableBitmap高效渲染
    wpf常用类型转换器,支持基元类型、可空基元类型、枚举
    wpf单位转换及DPI获取
    使用wpf技术实现画图工具
    InstallShield 创建 visual studio 工程的时候 指向 任意 visual studio 版本 方法 (修改 计算机 默认 visual studio shell 版本)
    WPF ScrollViewer(滚动条) 自定义样式表制作 再发一套样式 细节优化
    C#实现屏幕指定区域截屏
  • 原文地址:https://www.cnblogs.com/HJiang/p/4344398.html
Copyright © 2020-2023  润新知