zoukankan      html  css  js  c++  java
  • 添加大头针

    大头针的基本操作
    添加一个大头针
    - (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
  • 相关阅读:
    学习淘宝指数有感
    STL学习小结
    Java里泛型有什么作用
    android 内存泄漏分析技巧
    学道1.3
    严苛模式(StrictMode)
    年龄大了还能够学习编程吗
    ORACLE EXP命令
    数学之路-python计算实战(13)-机器视觉-图像增强
    《C语言编写 学生成绩管理系统》
  • 原文地址:https://www.cnblogs.com/HJiang/p/4344398.html
Copyright © 2011-2022 走看看