首先需要自定义一个包含经纬度,title,subtitle的数据模型

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h> @interface NearBrandAnnotation : NSObject<MKAnnotation> @property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle; @end
#import "NearBrandAnnotation.h"

@implementation NearBrandAnnotation

@end

在controller中需要以下数据

//地图
#import "NearBrandAnnotation.h"
#import <MapKit/MapKit.h>
#import "MapAnnotation.h"

遵守以下协议

@interface NearBrandStoreVC ()<MKMapViewDelegate,UIActionSheetDelegate>{
MKMapView *mapView;
MKAnnotationView *annotationView;
}

有以下属性

@property (nonatomic, assign) CLLocationDegrees strLatitude;//经度
@property (nonatomic, assign) CLLocationDegrees strLongitude;//维度

开始流程

1.创建地图

#pragma mark -- 地图
//标注目的地
- (void)createMap{
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, _safeInsets.top+44+47, SCREEN_WIDTH, SCREEN_HEIGHT-_safeInsets.top-44-_safeInsets.bottom-47)];
[self.view addSubview:mapView];
mapView.hidden = YES; mapView.delegate = self;
//用户位置追踪(用户位置追踪用于标记用户当前位置,此时会调用定位服务)
mapView.userTrackingMode = MKUserTrackingModeFollow;
//设置地图类型
mapView.mapType=MKMapTypeStandard;
//添加大头针 }

2.这是地图的代理方法之一-自定义大头针的样式,以及每个大头针的赋值

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    //由于当前位置的标注也是一个大头针,所以此时需要判断,此代理方法返回nil使用默认大头针视图
if ([annotation isKindOfClass:[NearBrandAnnotation class]]) {
static NSString *key1=@"NearBrandAnnotation";
annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:key1];
//如果缓存池中不存在则新建
if (!annotationView) {
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key1];
annotationView.canShowCallout=YES;//允许交互点击
annotationView.calloutOffset=CGPointMake(0, 1);//定义详情视图偏移量
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
btn.backgroundColor = BLUECOLOR;
[btn setTitle:@"到这去" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=btn;//定义详情左侧视图
annotationView.selected = YES;
objc_setAssociatedObject(btn, @"annotation",annotation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
//修改大头针视图
//重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的,位置是放到缓存池时的位置)
annotationView.annotation=annotation;
annotationView.image=[UIImage imageNamed:@"brandPin"];//设置大头针视图的图片 return annotationView;
}else {
return nil;
}
}

下面是我们自己的列表数据---以及如何将地图的数据加载

- (void)addAnnotations{
NSMutableArray *addAnnotations = [NSMutableArray new];
for (int i = 0; i< _nearBrandStores.count; i++) {
YuQuanBrandJiaMengShangListModel *mdoel = _nearBrandStores[i];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(mdoel.last_latitude.floatValue, mdoel.last_longitude.floatValue); NearBrandAnnotation *annotation=[[NearBrandAnnotation alloc]init];
annotation.title= mdoel.store_name;
annotation.subtitle= mdoel.address;
annotation.coordinate=location; // [mapView setCenterCoordinate:location zoomLevel:10 animated:NO];
[addAnnotations addObject:annotation];
}
[mapView addAnnotations:addAnnotations];
}

其中,需要注意的是,数据的绑定,每个大头针对应一个我们获取到的数据模型,用于点击跳转的时候使用

- (void)turnAction:(UIButton *)button{
NearBrandAnnotation *annotation = objc_getAssociatedObject(button, @"annotation");
_model = [YuQuanBrandJiaMengShangListModel new];
_model.address = annotation.subtitle;
_model.last_longitude = [NSString stringWithFormat:@"%f",annotation.coordinate.longitude];
_model.last_latitude = [NSString stringWithFormat:@"%f",annotation.coordinate.latitude];
[self mapBtnclick];
}

下面就是上次的跳转三方地图了

- (void)mapBtnclick{
if (![NSString isNotEmptyString:_currentadress]) {
[self.locationManager startUpdatingLocation];
[SDIndicator showInfoWithMessage:@"正在定位,请稍候..."];
return;
}
_actionSheet= [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil];
NSMutableArray *mapsArray=[[NSMutableArray alloc] initWithCapacity:0];
_mapsUrlArray=[[NSMutableArray alloc] init];
NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
[mapsArray addObject:@"使用iPhone自带地图"];
NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",_strLatitude,_strLongitude,[_model.last_latitude floatValue],[_model.last_longitude floatValue] ];
[_mapsUrlArray addObject:urlString];
} NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"];
if ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {
[mapsArray addObject:@"使用百度地图"]; NSString *stringURL =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",[_model.last_latitude floatValue],[_model.last_longitude floatValue],_model.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[_mapsUrlArray addObject:stringURL]; } NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"];
if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
[mapsArray addObject:@"使用高德地图"];
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",@"龙巅鱼邻",_strLatitude,_strLongitude,_currentadress,[_model.last_latitude floatValue],[_model.last_longitude floatValue],_model.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[_mapsUrlArray addObject:urlString]; } for (int x=0; x<mapsArray.count; x++) {
[_actionSheet addButtonWithTitle:[mapsArray objectAtIndex:x]]; } if (_mapsUrlArray.count>0) {
[_actionSheet showInView:self.view.window];
}else{
[SDIndicator showInfoWithMessage:@"建议您安装高德或者百度地图"];
} }

当然,跳转需要定位自己的经纬度,这个上一篇讲过了

iOS地图多个自定义大头针绘制核心代码的更多相关文章

  1. Android定位&地图&导航——基于百度地图,实现自定义图标绘制并点击时弹出泡泡

    一.问题描述 上一次我们使用百度地图实现基本的定位功能,接下来我们继续实现搜索和定位,并使用LocationOverlay绘制定位位置,同时展示如何使用自定义图标绘制并点击时弹出泡泡 如图所示: 二. ...

  2. [ios]quartz2d画板功功能实现核心代码

    //触摸开始 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //    1,获取对 ...

  3. SkylineGlobe 6.6 三维地图上实现自定义右键菜单示例代码

    1.OnRButtonDown.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  4. iOS原生地图开发指南续——大头针与自定义标注

    iOS原生地图开发指南续——大头针与自定义标注 出自:http://www.sxt.cn/info-6042-u-7372.html 在上一篇博客中http://my.oschina.net/u/23 ...

  5. iOS 地图定位及大头针的基本使用

    地图 Part1 - 定位及大头针的基本使用 一.MapKit 作用 : 用于地图展示 如大头针,路线,覆盖层展示等(着重界面展示) 使用步骤 导入头文件 #import <MapKit/Map ...

  6. 百度地图jsapi 自定义大头针的方法

    百度地图jsapi 自定义大头针的方法<pre> var myIcon = new BMap.Icon("http://developer.baidu.com/map/jsdem ...

  7. iOS地图

    地图 1.主要用到了地图展示和定位功能 CoreLocation框架的使用: 导入头文件        #import <CoreLocation/CoreLocation.h>CoreL ...

  8. 如何在iOS地图上高效的显示大量数据

    2016-01-13 / 23:02:13 刚才在微信上看到这篇由cocoachina翻译小组成员翻译的文章,觉得还是挺值得参考的,因此转载至此,原文请移步:http://robots.thought ...

  9. iOS 地图

      MapKit框架使用前提 导入框架 导入主头文件 #import <MapKit/MapKit.h> MapKit框架使用须知 MapKit框架中所有数据类型的前缀都是MK MapKi ...

随机推荐

  1. C++学习网站——www.cplusplus.com

    http://www.cplusplus.com/ 有各个函数.语法的实例代码,可以在线运行http://cpp.sh/不支持中文字符,不错.

  2. 统计C语言关键字出现次数

    统计C语言关键字出现次数 <C程序设计语言>K&R版本第6章结构6.3结构数组内容 /* Name: 统计c语言关键字出现次数 Copyright: Author: lingr7 ...

  3. 笔记-python-standard library-8.1 data types-datetime

    笔记-python-standard library-8.1 data types-datetime 1.      Datatimes 本章节内容为日期和时间处理类和方法. 1.1.    date ...

  4. char* 与char[]区别

    [转] 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区(认为无区别),导致有时出现“难以理解”的错误.一时也不能说得很明白,网上也 ...

  5. Windows网络编程笔记4 -- Winsock 协议相关知识

     Win32平台上的Winsock编程,Winsock是一个与协议无关的接口.以下协议是我们需要了解的: 网络协议的特征包括: 1.  面向消息 2.  面向连接和无线接 3.  可靠性和次序性 4. ...

  6. 如何过滤adb logcat输出

    简介: 本文介绍如何在 shell 命令行中过滤 adb logcat 输出的几个小技巧. 开发当中经常看到别人的 log 如洪水般瞬间刷满了屏幕,对自己有用的信息都被淹没了,影响心情也影响效率.下面 ...

  7. [python][django学习篇][5]选择数据库版本(默认SQLite3) 与操作数据库

    推荐学习博客:http://zmrenwu.com/post/6/ 选择数据库版本(SQLite3) 如果想选择MySQL等版本数据库,请先安装MySQL并且安装python mysql驱动,这里不做 ...

  8. 再次理解javascript的apply

    普通函数执行的时候,this指向函数执行的上下文  其实就是一个原型链的结构...    我一直没有搞懂原型链莫非它们像链条一样连在一起?    昂...   原型链可以理解成继承吗?   就像,ja ...

  9. 【转】UGUI(小地图的实现)与游戏关卡选择的简单实现

    http://www.jianshu.com/p/68637029e9df 游戏中小地图的实现(场景用简单Cube组成先搭建如下图场景,真实场景实现方法也是一样) 图1-1小地图效果图 1.创建好场景 ...

  10. 关于JavaWeb开发的一些感悟

    从事JavaWeb的开发已经三年了,从最开始的啥都不会,到慢慢的能够独立做项目,从一开始的一片茫然,到现在的心中有数.对于技术.业务也有了自己的看法. JavaWeb开发所涉及到的知识点非常多,涉及到 ...