终于效果图:

右下角的回到用户位置button:

MapController控制器,

是主控制器左側dock上面的【地图】button相应的控制器,

继承自ShowDealDetailController,

因此,自己主动拥有了展示团购详情控制器的功能

  1. //
  2. // MapController.h
  3. // 帅哥_团购
  4. //
  5. // Created by beyond on 14-8-14.
  6. // Copyright (c) 2014年 com.beyond. All rights reserved.
  7. // dock上面的【地图】button相应的控制器,继承自ShowDealDetailController.h控制器,自己主动拥有了展示团购详情控制器的功能了
  8.  
  9. #import "ShowDealDetailController.h"
  10.  
  11. @interface MapController : ShowDealDetailController
  12.  
  13. @end
  1. //
  2. // MapController.m
  3. // 帅哥_团购
  4. //
  5. // Created by beyond on 14-8-14.
  6. // Copyright (c) 2014年 com.beyond. All rights reserved.
  7. // dock上面的【地图】button相应的控制器,继承自ShowDealDetailController.h控制器,自己主动拥有了展示团购详情控制器的功能了
  8.  
  9. #import "MapController.h"
  10. #import <MapKit/MapKit.h>
  11. #import "DealRequestTool.h"
  12. #import "MetaDataTool.h"
  13. #import "LocationTool.h"
  14. // 成员有经纬度坐标
  15. #import "City.h"
  16. // 一个商户模型
  17. #import "Business.h"
  18. #import "Deal.h"
  19. // 一个大头针模型,为大头针View提供数据源的
  20. #import "MyAnnotation.h"
  21. // 跨的经度和纬度
  22. #define kSpan MKCoordinateSpanMake(0.018404, 0.031468)
  23.  
  24. @interface MapController ()<MKMapViewDelegate>
  25. {
  26. MKMapView *_mapView;
  27. NSMutableArray *_showingDeals;
  28. }
  29. @end
  30.  
  31. @implementation MapController
  32.  
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  36.  
  37. self.title = @"地图";
  38. // 0.监听定位完毕的通知.....这里面有问题
  39. kAddAllNotes(dataChange)
  40.  
  41. // 1.加入地图
  42. MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  43. mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  44. // 显示用户的位置点
  45. mapView.showsUserLocation = YES;
  46. // 设置代理
  47. mapView.delegate = self;
  48. [self.view addSubview:mapView];
  49.  
  50. // 2.初始化数组
  51. _showingDeals = [NSMutableArray array];
  52.  
  53. // 3.加入回到用户位置的button
  54. [self addBackToUserLocationBtn];
  55.  
  56. }
  57.  
  58. - (void)addBackToUserLocationBtn
  59. {
  60. // 3.加入回到用户位置的button
  61. UIButton *backUserBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  62. CGSize imgSize = [backUserBtn setBtnBgImgForNormal:@"btn_map_locate.png" highlightedName:@"btn_map_locate_hl.png"];
  63. CGFloat w = imgSize.width;
  64. CGFloat h = imgSize.height;
  65. CGFloat margin = 20;
  66. CGFloat x = self.view.frame.size.width - w - margin;
  67. CGFloat y = self.view.frame.size.height - h - margin;
  68. // button处于右下角所以左边距和上边距自己主动伸缩
  69. backUserBtn.frame = CGRectMake(x, y, w, h);
  70. backUserBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
  71. [backUserBtn addTarget:self action:@selector(backToUserLocationBtnClicked) forControlEvents:UIControlEventTouchUpInside];
  72. [self.view addSubview:backUserBtn];
  73. }
  74.  
  75. // 返回到用户中心点button被点击
  76. - (void)backToUserLocationBtnClicked
  77. {
  78. // mapView里面保存着用户的位置信息(The annotation representing the user's location)
  79. CLLocationCoordinate2D center = _mapView.userLocation.location.coordinate;
  80. MKCoordinateRegion region = MKCoordinateRegionMake(center, kSpan);
  81. // 设置中心点
  82. [_mapView setRegion:region animated:YES];
  83. }
  84.  
  85. #pragma mark - mapView的代理方法
  86. #pragma mark 当定位到用户的位置就会调用(调用频率相当高)---2
  87. - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
  88. {
  89. // 仅仅让它首次定位到用户坐标
  90. if (_mapView) return;
  91.  
  92. // 1.用户位置的(中心点)
  93. CLLocationCoordinate2D center = userLocation.location.coordinate;
  94.  
  95. // 2.确定好中心点之后,再确定跨度(范围)
  96. // MKCoordinateSpan span = MKCoordinateSpanMake(0.018404, 0.031468);
  97.  
  98. // 3.依据中心点和跨度之后,就确定好了区域
  99. MKCoordinateRegion region = MKCoordinateRegionMake(center, kSpan);
  100.  
  101. // 4.让mapView显示到指定的区域
  102. [mapView setRegion:region animated:YES];
  103. _mapView = mapView;
  104.  
  105. // 设置中心点坐标 为用户的坐标...
  106. // [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
  107.  
  108. }
  109.  
  110. #pragma mark 拖动地图(地图展示的区域改变了)就会调用
  111. - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
  112. {
  113.  
  114. // 1.地图当前展示区域的中心位置
  115. CLLocationCoordinate2D pos = mapView.region.center;
  116.  
  117. // 依据中心点位置坐标,反算出城市名,
  118.  
  119. // 2.利用工具类向server发送请求
  120. [[DealRequestTool sharedDealRequestTool] dealsRequestWithPos:pos success:^(NSArray *deals, int total_count) {
  121.  
  122. // 遍历返回的deals数组,并与当前控制器保存的已经显示过的deals数组比較,假设,已经显示过该deal,则continue跳过,
  123. for (Deal *d in deals) {
  124. // 已经显示过,跳过,避免 大头针 影子加深
  125. if ([_showingDeals containsObject:d]) continue;
  126.  
  127. // 假设返回的deal,从未显示过,先加到成员数组中,然后,将该团购的成员:商区,一一用大头针进行显示到mapView上面
  128. [_showingDeals addObject:d];
  129.  
  130. // 遍历 该团购的商户对象数组
  131. for (Business *b in d.businesses) {
  132. // 一个商户相应一个大头针模型,也就是数据源,为Annotation View提供数据
  133. // Annotation 是 模型,用来在map上标记 坐标
  134. // 实现代理的 mapView:viewForAnnotation: 方法,返回每个Annotation相应的Annotation View
  135. MyAnnotation *anno = [[MyAnnotation alloc] init];
  136. anno.business = b;
  137. anno.deal = d;
  138. anno.coordinate = CLLocationCoordinate2DMake(b.latitude, b.longitude);
  139. // 重要~~~ 为mapView提供数据,接着会来到方法mapView:viewForAnnotation:
  140. [mapView addAnnotation:anno];
  141. }
  142. }
  143.  
  144. } error:^(NSError *error) {
  145. log(@"error---%@",error);
  146. }];
  147.  
  148. }
  149. #pragma mark - mapView的代理方法
  150. // 相似于 cell For Row,为每个annotation 提供view,这里使用了自己定义的Annotation模型
  151. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyAnnotation *)annotation
  152. {
  153. if (![annotation isKindOfClass:[MyAnnotation class]]) return nil;
  154.  
  155. // 1.从缓存池中取出大头针view
  156. static NSString *ID = @"MKAnnotationView";
  157. MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
  158.  
  159. // 2.缓存池没有可循环利用的大头针view
  160. if (annoView == nil) {
  161. // 这里应该用MKPinAnnotationView这个子类,一个在构造annotationView时,必须提供数据源模型annotation
  162. annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
  163. }
  164.  
  165. // 3.设置view的大头针信息,提供独一无二的数据源模型
  166. annoView.annotation = annotation;
  167.  
  168. // 4.设置图片
  169. annoView.image = [UIImage imageNamed:annotation.icon];
  170.  
  171. return annoView;
  172. }
  173.  
  174. #pragma mark 点击了大头针
  175. - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
  176. {
  177. // 0.假设是系统自带的大头针,则直接返回...
  178. if (![view.annotation isKindOfClass:[MyAnnotation class]]) {
  179. return;
  180. }
  181.  
  182. // 1.调用父类的方法,展示详情控制器,并提供数据源
  183. MyAnnotation *anno = view.annotation;
  184. [self showDetail:anno.deal];
  185.  
  186. // 2.让选中的大头针居中(成为中心点)
  187. [mapView setCenterCoordinate:anno.coordinate animated:YES];
  188.  
  189. // 3.让view周边产生一些阴影效果
  190. view.layer.shadowColor = [UIColor blueColor].CGColor;
  191. view.layer.shadowOpacity = 1;
  192. view.layer.shadowRadius = 10;
  193. }
  194.  
  195. #pragma mark - 监听到定位城市发生改变时,又一次刷新webView
  196. // 0.监听定位完毕的通知
  197. - (void)dataChange
  198. {
  199. // 1.城市对象
  200. City *city = [MetaDataTool sharedMetaDataTool].currentCity;
  201. CLGeocoder *geo = [[CLGeocoder alloc] init];
  202. // 全球搜索 某某城市
  203. [geo geocodeAddressString:city.name completionHandler:^(NSArray *placemarks, NSError *error) {
  204.  
  205. // 定位,解析完毕之后,就能够提供经纬度
  206. CLPlacemark *place = placemarks[0];
  207. // 1.用户位置的(中心点)
  208. CLLocationCoordinate2D center = place.location.coordinate;
  209. city.position = place.location.coordinate;
  210. // 重要,将城市用工具记住,由于发送请求时,还用到了城市 名和 经纬度
  211. [LocationTool sharedLocationTool].locationCity = city;
  212. // 2.确定好中心点之后,再确定跨度(范围)
  213. // 3.依据中心点和跨度之后,就确定好了区域
  214. MKCoordinateRegion region = MKCoordinateRegionMake(center, kSpan);
  215. // 4.让mapView显示到指定的区域
  216. [_mapView setRegion:region animated:YES];
  217. }];
  218. }
  219. @end

iOS_21团购_地图功能的更多相关文章

  1. iOS_21团购_发送请求【点评】数据

    结果表明,一个简单的请求: 用到的点评封装的类: 使用tableView简单展示: // // DealListController.m // 帅哥_团购 // // Created by beyon ...

  2. iOS_21团购_顶部菜单和弹出菜单联动

    最后效果图: 各控件关系图1: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize ...

  3. iOS_21团购_Popover适应iPad横竖屏切换

    终于效果图: 代码片段: // // DockItemLocation.m // 帅哥_团购 // // Created by beyond on 14-8-13. // Copyright (c) ...

  4. HER COFFEE夜场代金券【1折】_北京美食团购_360团购导航

    HER COFFEE夜场代金券[1折]_北京美食团购_360团购导航 HER COFFEE夜场代金券

  5. ecshop 团购点击价格变动

    前提:价格阶梯只能设置一级 需要用到: jquery,transport.js(transport_jquery.js),Ajax.call html页面 js代码,还需要插入jquery,trans ...

  6. iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例

    一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...

  7. IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

  8. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  9. 【团购活动】接口最全最好用的S5PV210开发板Sate210-F 开发板开始团购活动了,一起学习linux!

    接口最全最好用的S5PV210开发板Sate210-F 开发板开始团购活动了,一起学习linux!http://bbs.eeworld.com.cn/forum.php?mod=viewthread& ...

随机推荐

  1. mybits 操作指南

    第一.一对一: <resultMap type="com.zktx.platform.entity.tb.Module" id="BaseResultMap&quo ...

  2. 自定义控件三部曲之动画篇(四)——ValueAnimator基本使用

    前言:不要让别人的无知断送了你的梦想,永远坚信你所坚信的. 相关文章: <Android自定义控件三部曲文章索引>:http://blog.csdn.net/harvic880925/ar ...

  3. [C#] override和overload的区别

    重载应该叫overload,重写叫override:重载某个方法是在同一个类中发生的!重写是在子类中重写父类中的方法. 1.override:   父类:public virtual string T ...

  4. vcpkg错误分析方法

    最近在使用vcpkg时,经常会碰到CMake错误. 有些以前能编译通过的包, 过一段时间又不能编译错误了. 错误提示一般是CMake错误, 弄得很郁闷. 我采用以下步骤解决了问题: 分析错误 查看错误 ...

  5. A - Translation

    Problem description The translation from the Berland language into the Birland language is not an ea ...

  6. 第一天:java与mysql的连接工具类

    第一天:java与mysql的连接工具类 java最新版马上就要收费,这无疑是这门语言的衰败起始,毕竟在中国收费便难发展,例如c#,但是毕业设计已经选好用java来写一个动态网站, 这已经是一个事实, ...

  7. 第三课 创建函数 - 从EXCEL读取 - 导出到EXCEL - 异常值 - Lambda函数 - 切片和骰子数据

    第 3 课   获取数据 - 我们的数据集将包含一个Excel文件,其中包含每天的客户数量.我们将学习如何对 excel 文件进​​行处理.准备数据 - 数据是有重复日期的不规则时间序列.我们将挑战数 ...

  8. Android学习——Button填充颜色及实现圆角

    在drawable下新建文件夹bt_shape.xml,如下: <?xml version="1.0" encoding="utf-8"?> < ...

  9. 应用Struts2框架,开发一个加法器,采用两个页面,一个页面输入数据,另一个界面输出结果。

    软件152谭智馗 一.新建maven项目 1.选择菜单file—new—maven project,勾选“Create a &simple project (skip archetype se ...

  10. java是值传递,还是引用传递?

    原文地址:http://blog.csdn.net/zxmzfbdc/article/details/5401960  java到底是值传递,还是引用传递?以前国内的java开发者有过很多争论,由于& ...