如何使用地图的注解功能

如何使用地图的注解功能

实现了协议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

时间: 2024-12-02 04:24:52

如何使用地图的注解功能的相关文章

百度地图全屏功能怎么用

  百度地图全屏功能使用方法:点击地图右上角的全屏按钮,即可进入全屏状态,点击退出全屏或Esc退出. 百度地图搜索是百度提供的一项网络地图搜索服务,覆盖了国内近400个城市.数千个区县.在百度地图里,您可以查询街道.商场.楼盘的地理位置,也可以找到离您最近的所有餐馆.学校.银行.公园等等.

ios开发-高德地图的搜索功能的实现

问题描述 高德地图的搜索功能的实现 我这里项目的需要是,使用高德地图, 要把地图上距离我当前位置这个大头针显示的位置的2公里之内的住宿,酒店,餐饮的信息, 全部检索到并显示在地图上, 我这里在项目里面要显示结果的地方添加的代码如下, //初始化检索对象 _searchMapView = [[AMapSearchAPI alloc] initWithSearchKey:@"549f6a2bfd98c54aaf000c6f" Delegate:self]; if ([kCurentLang

link环境下制作一款《订餐软件》,使用百度地图对接api功能,订餐取餐显示订单怎么做?

问题描述 link环境下制作一款<订餐软件>,使用百度地图对接api功能,订餐取餐显示订单怎么做? link环境下制作一款<订餐软件>,使用百度地图对接api功能,订餐取餐显示订单怎么做? 解决方案 可以做成二维码,这样总台扫描下就可以知道了.

我的Android进阶之旅------&amp;gt;Android百度地图定位SDK功能学习

          因为项目需求,需要使用百度地图的定位功能,因此去百度地图开发平台下载了百度地图的Android定位SDK最新版本的开发包和示例代码学习.       Android 定位SDK地址:http://developer.baidu.com/map/index.php?title=android-locsdk      下载地址为:http://developer.baidu.com/map/index.php?title=android-locsdk/geosdk-android

java注解功能的实现 求大神赐教

问题描述 java注解功能的实现 求大神赐教 如何定义一个注解去out把它写在某个方法的前面 使得可以直接运行该方法 相当于main中写了个该方法 如何实现???.. 解决方案 在main中反射,遍历方法,发现有你定义的注解,就执行.

genfence-百度地图—地理围栏功能

问题描述 百度地图-地理围栏功能 导入的包是百度云平台下载的最新版的,但是在Activity中找不到GeofenceClient这个类~这是怎么回事?求大神指点~ 解决方案 //创建围栏对象 mGeofenceClient = new GeofenceClient(getApplicationContext()); //注册围栏监听并开始围栏扫描服务 mGeofenceClient.registerGeofenceTriggerListener(new GeofenceClient.OnGeof

hiberna-使用ehcache-spring-annotations开启ehcache的注解功能

问题描述 使用ehcache-spring-annotations开启ehcache的注解功能 使用ehcache-spring-annotations开启ehcache的注解功能时, eclipse提示如下问题,求教怎么搞定~~~~ 解决方案 EHcache注解使用(ehcache-spring-annotations)spring 2.5 annotations配置ehcache在Spring中快速使用EHCache注解 解决方案二: 是不是没写 http://ehcache-spring-

多人出游必备良品:高德地图上线组队功能

本文讲的是多人出游必备良品:高德地图上线组队功能最近,高德地图独家上线了一个"组队"功能,通过相互共享彼此的实时位置来帮助用户快速抵达目的地.在亲自测试之后,笔者惊喜地发现这个新功能,非常适合多人出游,尤其是自驾游的场景. 因为自驾游的时候肯定要用到地图导航APP,既然能直接在地图里分享彼此位置,就不用再打开微信建个临时群来分享位置,更免了在导航和微信之间来回切换的烦恼. 调用高德地图"组队"功能十分简单,首先将高德地图升级到最新版本,然后在地图首页界面右上角的工具

IOS百度地图导航开发功能实现简述_IOS

以下通过图文并茂的方式给大家讲述百度地图导航开发功能: 第一步:在使用百度导航之前,我们需要在百度地图开放平台上下载导航的 SDK,共85.8M,网速不好的同学可提前准备好. 第二步:引入导航所需的系统包 将AudioToolbox.framework.ImageIO.framework.CoreMotion.framework.CoreLocation.framework.CoreTelephony.framework.MediaPlayer.framework.AVFoundation.fr