如何使用地图的注解功能

简介:

如何使用地图的注解功能

实现了协议MKAnnotation的对象才能够被用地图上的注解对象,一般都是如下定义的.

DisplayMap.h + DisplayMap.m

//
//  DisplayMap.h
//  MoreMapInfo
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface DisplayMap : NSObject <MKAnnotation>

@property (nonatomic, copy, readonly)   NSString                *title;
@property (nonatomic, copy, readonly)   NSString                *subtitle;
@property (nonatomic, assign, readonly) CLLocationCoordinate2D   coordinate;

- (id)initWithTitle:(NSString*)title
           SubTitle:(NSString*)subtitle
         Coordinate:(CLLocationCoordinate2D)coordinate;

@end


//
//  DisplayMap.m
//  MoreMapInfo
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "DisplayMap.h"

@implementation DisplayMap

- (id)initWithTitle:(NSString*)title
           SubTitle:(NSString*)subtitle
         Coordinate:(CLLocationCoordinate2D)coordinate
{
    if (self = [super init])
    {
        _title      = title;
        _subtitle   = subtitle;
        _coordinate = coordinate;
    }
    
    return self;
}

@end

RootViewController.m
//
//  RootViewController.m
//  MoreMapInfo
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import <MapKit/MapKit.h>
#import "DisplayMap.h"
#import "LocationCoder.h"
#import "YXLocationManager.h"

@interface RootViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>

@property (nonatomic, strong) MKMapView  *mapView;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [YXLocationManager getCurrentLocation:^(CLLocation *location, NSError *error) {
        if (error == nil)
        {
            // 将经纬度解析为地址
            LocationCoder *lCoder = [[LocationCoder alloc] initWithLocation:location];
            [lCoder startAnalyseLocation];
            lCoder.resultBlock = ^(NSArray *placemarks, NSError *error,
                                   LocationCoder *locationCoder)
            {
                NSLog(@"%@", locationCoder.addressLines);
            };
            
            //地图
            _mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
            _mapView.delegate          = self;
            _mapView.showsUserLocation = YES;
            _mapView.mapType           = MKMapTypeHybrid;
            _mapView.region = MKCoordinateRegionMake(location.coordinate,
                                                     MKCoordinateSpanMake(0.1, 0.1));
            
            //加入大头针
            DisplayMap *anno = [[DisplayMap alloc]initWithTitle:@"title"
                                                       SubTitle:@"subtitle"
                                                     Coordinate:CLLocationCoordinate2DMake(39.928168, 116.39328)];
            [_mapView addAnnotation:anno];
            [self.view addSubview:_mapView];
            
            //给地图添加长按手势,插上大头针
            UILongPressGestureRecognizer* longPress = \
            [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                          action:@selector(longPress:)];
            [_mapView addGestureRecognizer:longPress];
        }
        else
        {
            NSLog(@"%@", error);
        }
    }];
}

#pragma mark - viewDidLoad UILongPressGestureRecognize longPress:
- (void)longPress:(UILongPressGestureRecognizer*)longPress
{
    //长按一次 只在开始插入一次大头针   否则能用大头针写字。。。
    if (longPress.state == UIGestureRecognizerStateBegan)
    {
        CGPoint point = [longPress locationInView:_mapView];
        CLLocationCoordinate2D coordinate = [_mapView convertPoint:point
                                              toCoordinateFromView:_mapView];
        
        
        DisplayMap* an = [[DisplayMap alloc] initWithTitle:@"title"
                                                  SubTitle:@"subtitle"
                                                Coordinate:coordinate];
        [_mapView addAnnotation:an];
        
        [_mapView setCenterCoordinate:coordinate
                             animated:YES];
    }
}

#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //如果是所在地,跳过(固定写法)
    if ([annotation isKindOfClass:[mapView.userLocation class]])
    {
        return nil;
    }
    
    // 重用
    MKPinAnnotationView* pinView = (MKPinAnnotationView*)\
        [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
    if (pinView == nil)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                  reuseIdentifier:@"ID"];
    }
    
    //能显示Call信息上面那些图字(很重要哦)
    pinView.canShowCallout = YES;
    
    //只有三种
    pinView.pinColor = MKPinAnnotationColorPurple;

    //显示动画 - 从天上落下
    pinView.animatesDrop = YES;
    
    pinView.image = [UIImage imageNamed:@"地图标点"];
    
    // 左侧CalloutAccessoryView(使用了自定义view)
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    view.backgroundColor = [UIColor redColor];
    pinView.leftCalloutAccessoryView = view;
    
    // 右侧CalloutAccessoryView(使用了系统的按钮)
    UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = button;
    
    return pinView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"点击事件");
}

@end

打印信息:

2014-05-26 10:24:46.005 MoreMapInfo[6138:60b] AKLocation - [Line 118] 
 Started locating
 ==============
 Distance filter accuracy: -1.00
 Desired accuracy: 100.00
 Timeout: 10.00
May 26 10:24:47 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:47.331 MoreMapInfo[6138:9c07] vImage decode failed, falling back to CG path.
May 26 10:24:47 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:47.372 MoreMapInfo[6138:8f0b] vImage decode failed, falling back to CG path.
2014-05-26 10:24:47.462 MoreMapInfo[6138:60b] 中国北京市东城区东四街道东四朝阳门北小街2-1号
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.363 MoreMapInfo[6138:a803] vImage decode failed, falling back to CG path.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.375 MoreMapInfo[6138:a603] vImage decode failed, falling back to CG path.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.402 MoreMapInfo[6138:9607] vImage decode failed, falling back to CG path.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.420 MoreMapInfo[6138:910b] vImage decode failed, falling back to CG path.
2014-05-26 10:24:48.421 MoreMapInfo[6138:9e07] vImage decode failed, falling back to CG path.
2014-05-26 10:25:10.317 MoreMapInfo[6138:60b] 点击事件

 

要点:

// 能显示Call信息上面那些图字(很重要哦)
pinView.canShowCallout = YES

目录
相关文章
|
2月前
|
JavaScript Java
若依框架 - - - 自定义打印模版
若依框架 - - - 自定义打印模版
75 0
若依框架 - - - 自定义打印模版
|
9月前
|
定位技术
高德地图进阶开发实战案例(3):标注、信息弹窗和清除覆盖物
高德地图进阶开发实战案例(3):标注、信息弹窗和清除覆盖物
102 0
|
9月前
|
移动开发 JavaScript 前端开发
数据可视化大屏百度地图手机端标注开发实战案例解析(jsAPI接口、标注分类图片、文本标签、分类筛选、自适应高度信息弹窗、PHP后端API)
数据可视化大屏百度地图手机端标注开发实战案例解析(jsAPI接口、标注分类图片、文本标签、分类筛选、自适应高度信息弹窗、PHP后端API)
147 0
|
4月前
|
IDE 定位技术 开发工具
百度地图如何创建一个属于自己的地图,附加到项目中?
百度地图如何创建一个属于自己的地图,附加到项目中?
62 0
|
9月前
|
前端开发 定位技术
地图开发实战案例:高德地图的标注、信息窗口的实战实例
地图开发实战案例:高德地图的标注、信息窗口的实战实例
117 0
|
前端开发
前端学习案例-自定义navLink&自定义导航1
前端学习案例-自定义navLink&自定义导航1
59 0
前端学习案例-自定义navLink&自定义导航1
|
数据处理 C#
基于C#的ArcEngine二次开发39:GP工具的使用--界面、参数及示例代码
基于C#的ArcEngine二次开发39:GP工具的使用--界面、参数及示例代码
基于C#的ArcEngine二次开发39:GP工具的使用--界面、参数及示例代码
|
JavaScript 前端开发 定位技术
Vue 集成腾讯地图基础api Demo集合(基础地图引入与展示模块,地址逆解析,3D/2D切换 ,位置服务,mark标记)
Vue 集成腾讯地图基础api Demo集合(基础地图引入与展示模块,地址逆解析,3D/2D切换 ,位置服务,mark标记)
844 0
Vue 集成腾讯地图基础api  Demo集合(基础地图引入与展示模块,地址逆解析,3D/2D切换 ,位置服务,mark标记)
|
JavaScript 前端开发 Go
Google地图官方API- 在地图上绘图(自定义标记)
var map; function initMap() { map = new google.maps.Map( document.