zoukankan      html  css  js  c++  java
  • MapsDemo

      1 #import "ViewController.h"
      2 //位置
      3 #import <CoreLocation/CoreLocation.h>
      4 //地图
      5 #import <MapKit/MapKit.h>
      6 //标注视图
      7 #import "HLAnnotation.h"
      8 @interface ViewController ()<MKMapViewDelegate>
      9 //位置的管理者
     10 @property (nonatomic,strong) CLLocationManager *manager ;
     11 //地图的对象
     12 @property (nonatomic,strong) MKMapView *mapView;
     13 
     14 @end
     15 
     16 @implementation ViewController
     17 
     18 - (void)viewDidLoad {
     19     [super viewDidLoad];
     20     
     21     //创建位置管理者
     22     self.manager = [[CLLocationManager alloc] init];
     23     if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
     24         
     25         [self.manager requestAlwaysAuthorization];
     26         [self.manager requestWhenInUseAuthorization];
     27     }
     28     //创建地图的对象
     29     self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
     30     [self.view addSubview:self.mapView];
     31     //地图的类型
     32     self.mapView.mapType = MKMapTypeStandard ;
     33     //显示用户的信息
     34     self.mapView.showsUserLocation = YES ;
     35     //设置地图的代理
     36     self.mapView.delegate = self ;
     37     
     38     //添加一个手势
     39     [self.mapView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGerture:)]];
     40    
     41 }
     42 
     43 #pragma maek --长按手势的关联方法
     44 -(void)longPressGerture:(UILongPressGestureRecognizer *)longPress
     45 {
     46     //获取点击点
     47     CGPoint point = [longPress locationInView:longPress.view];
     48     
     49     //将触摸得到的点,转化为2D坐标的点
     50     CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
     51     
     52     //创建一个标注视图对象
     53     HLAnnotation *annotation = [[HLAnnotation alloc] init];
     54     
     55     //给标注视图设置一个地图上的位置
     56     annotation.coordinate = coordinate ;
     57     
     58     //设置标题
     59     annotation.title = @"china";
     60     
     61     //设置子标题
     62     annotation.subtitle = @"广州";
     63     
     64     static NSInteger a = 1 ;
     65     
     66     //设置tag值
     67     annotation.tag = a ;
     68     a ++ ;
     69     
     70     //将标注视图添加到地图上
     71     [self.mapView addAnnotation:annotation];
     72     
     73 }
     74 #pragma mark --自定义标注视图样式的方法
     75 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
     76 {
     77     //如果系统的用户位置的大头针
     78     if ([annotation isKindOfClass:[MKUserLocation class]]) {
     79         
     80         return nil;
     81     }
     82     
     83     //创建一个重用标示符
     84     static NSString *identifier = @"annotation";
     85     
     86     //创建一个重用队列
     87     MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
     88     if (pin == nil) {
     89        
     90         pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation     reuseIdentifier:identifier];
     91     }
     92     //设置大头针的往下坠落的效果
     93     pin.animatesDrop = YES ;
     94     //允许打开气泡的属性
     95     pin.canShowCallout = YES ;
     96     
     97     //创建气泡的左右按钮
     98     UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
     99     leftButton.frame = CGRectMake(0, 0, 30, 30);
    100     [leftButton setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    101     pin.leftCalloutAccessoryView = leftButton ;
    102     leftButton.tag = 10086 ;
    103     
    104     UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    105     rightButton.frame = CGRectMake(0, 0, 30, 30);
    106     [rightButton setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];
    107     pin.rightCalloutAccessoryView = rightButton ;
    108     rightButton.tag = 10010 ;
    109 
    110     
    111     return pin ;
    112 }
    113 
    114 #pragma mark --点击大头针附件按钮时执行的方法
    115 -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    116 {
    117     if (control ==[self.view viewWithTag:10086]) {
    118         
    119         NSLog(@"左边");
    120     }
    121     else
    122     {
    123         NSLog(@"右边");
    124     }
    125 }
    126 #pragma mark --点击标注视图执行的方法
    127 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    128 {
    129     NSLog(@"view = %@",view);
    130 }
    131 
    132 #pragma mark --地图区域发生改变时执行的方法
    133 -(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
    134 {
    135     NSLog(@"地图区域发生改变时执行的方法");
    136 }
    137 
    138 #pragma mark --更新用户位置的方法
    139 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    140 {
    141     NSLog(@"%@",userLocation.location);
    142     
    143     //设置标题
    144     userLocation.title = @"广州市";
    145     userLocation.subtitle = @"天河区";
    146     //设置地图的比例尺
    147     MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    148     //设置范围
    149     MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    150     self.mapView.region = region ;
    151 }

    自定义标注视图:继承NSObject

     1 #import <Foundation/Foundation.h>
     2 #import <MapKit/MapKit.h>
     3 @interface HLAnnotation : NSObject<MKAnnotation>
     4 
     5 //MKAnnotation协议里面必须实现的属性
     6 @property (nonatomic) CLLocationCoordinate2D coordinate;
     7 
     8 //MKAnnotation协议里面的可选属性
     9 @property (nonatomic,  copy) NSString *title;
    10 @property (nonatomic,  copy) NSString *subtitle;
    11 
    12 //设置一个tag值标记区分每一个标注视图(大头针)
    13 @property (nonatomic) NSInteger tag ;
    14 
    15 @end
  • 相关阅读:
    洛谷 P1896 [SCOI2005]互不侵犯(状压DP)
    POJ 3208 Apocalypse Someday(数位DP)
    HDU 3555 Bomb(数位DP)
    HDU 3652 B-number(数位DP)
    蜂鸣器版天空之城
    【洛谷习题】小木棍[数据加强版]
    【NOIP2009】靶形数独
    【洛谷习题】填涂颜色
    【NOIP2003】加分二叉树
    【NOIP2000】单词接龙
  • 原文地址:https://www.cnblogs.com/yyxblogs/p/4872876.html
Copyright © 2011-2022 走看看