终于效果图:

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

MapController控制器,

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

继承自ShowDealDetailController,

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

//
// MapController.h
// 帅哥_团购
//
// Created by beyond on 14-8-14.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// dock上面的【地图】button相应的控制器,继承自ShowDealDetailController.h控制器,自己主动拥有了展示团购详情控制器的功能了 #import "ShowDealDetailController.h" @interface MapController : ShowDealDetailController @end
//
// MapController.m
// 帅哥_团购
//
// Created by beyond on 14-8-14.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// dock上面的【地图】button相应的控制器,继承自ShowDealDetailController.h控制器,自己主动拥有了展示团购详情控制器的功能了 #import "MapController.h"
#import <MapKit/MapKit.h>
#import "DealRequestTool.h"
#import "MetaDataTool.h"
#import "LocationTool.h"
// 成员有经纬度坐标
#import "City.h"
// 一个商户模型
#import "Business.h"
#import "Deal.h"
// 一个大头针模型,为大头针View提供数据源的
#import "MyAnnotation.h"
// 跨的经度和纬度
#define kSpan MKCoordinateSpanMake(0.018404, 0.031468) @interface MapController ()<MKMapViewDelegate>
{
MKMapView *_mapView;
NSMutableArray *_showingDeals;
}
@end @implementation MapController - (void)viewDidLoad
{
[super viewDidLoad]; self.title = @"地图";
// 0.监听定位完毕的通知.....这里面有问题
kAddAllNotes(dataChange) // 1.加入地图
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// 显示用户的位置点
mapView.showsUserLocation = YES;
// 设置代理
mapView.delegate = self;
[self.view addSubview:mapView]; // 2.初始化数组
_showingDeals = [NSMutableArray array]; // 3.加入回到用户位置的button
[self addBackToUserLocationBtn]; } - (void)addBackToUserLocationBtn
{
// 3.加入回到用户位置的button
UIButton *backUserBtn = [UIButton buttonWithType:UIButtonTypeCustom];
CGSize imgSize = [backUserBtn setBtnBgImgForNormal:@"btn_map_locate.png" highlightedName:@"btn_map_locate_hl.png"];
CGFloat w = imgSize.width;
CGFloat h = imgSize.height;
CGFloat margin = 20;
CGFloat x = self.view.frame.size.width - w - margin;
CGFloat y = self.view.frame.size.height - h - margin;
// button处于右下角所以左边距和上边距自己主动伸缩
backUserBtn.frame = CGRectMake(x, y, w, h);
backUserBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[backUserBtn addTarget:self action:@selector(backToUserLocationBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backUserBtn];
} // 返回到用户中心点button被点击
- (void)backToUserLocationBtnClicked
{
// mapView里面保存着用户的位置信息(The annotation representing the user's location)
CLLocationCoordinate2D center = _mapView.userLocation.location.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMake(center, kSpan);
// 设置中心点
[_mapView setRegion:region animated:YES];
} #pragma mark - mapView的代理方法
#pragma mark 当定位到用户的位置就会调用(调用频率相当高)---2
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// 仅仅让它首次定位到用户坐标
if (_mapView) return; // 1.用户位置的(中心点)
CLLocationCoordinate2D center = userLocation.location.coordinate; // 2.确定好中心点之后,再确定跨度(范围)
// MKCoordinateSpan span = MKCoordinateSpanMake(0.018404, 0.031468); // 3.依据中心点和跨度之后,就确定好了区域
MKCoordinateRegion region = MKCoordinateRegionMake(center, kSpan); // 4.让mapView显示到指定的区域
[mapView setRegion:region animated:YES];
_mapView = mapView; // 设置中心点坐标 为用户的坐标...
// [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES]; } #pragma mark 拖动地图(地图展示的区域改变了)就会调用
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{ // 1.地图当前展示区域的中心位置
CLLocationCoordinate2D pos = mapView.region.center; // 依据中心点位置坐标,反算出城市名, // 2.利用工具类向server发送请求
[[DealRequestTool sharedDealRequestTool] dealsRequestWithPos:pos success:^(NSArray *deals, int total_count) { // 遍历返回的deals数组,并与当前控制器保存的已经显示过的deals数组比較,假设,已经显示过该deal,则continue跳过,
for (Deal *d in deals) {
// 已经显示过,跳过,避免 大头针 影子加深
if ([_showingDeals containsObject:d]) continue; // 假设返回的deal,从未显示过,先加到成员数组中,然后,将该团购的成员:商区,一一用大头针进行显示到mapView上面
[_showingDeals addObject:d]; // 遍历 该团购的商户对象数组
for (Business *b in d.businesses) {
// 一个商户相应一个大头针模型,也就是数据源,为Annotation View提供数据
// Annotation 是 模型,用来在map上标记 坐标
// 实现代理的 mapView:viewForAnnotation: 方法,返回每个Annotation相应的Annotation View
MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.business = b;
anno.deal = d;
anno.coordinate = CLLocationCoordinate2DMake(b.latitude, b.longitude);
// 重要~~~ 为mapView提供数据,接着会来到方法mapView:viewForAnnotation:
[mapView addAnnotation:anno];
}
} } error:^(NSError *error) {
log(@"error---%@",error);
}]; }
#pragma mark - mapView的代理方法
// 相似于 cell For Row,为每个annotation 提供view,这里使用了自己定义的Annotation模型
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyAnnotation *)annotation
{
if (![annotation isKindOfClass:[MyAnnotation class]]) return nil; // 1.从缓存池中取出大头针view
static NSString *ID = @"MKAnnotationView";
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID]; // 2.缓存池没有可循环利用的大头针view
if (annoView == nil) {
// 这里应该用MKPinAnnotationView这个子类,一个在构造annotationView时,必须提供数据源模型annotation
annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
} // 3.设置view的大头针信息,提供独一无二的数据源模型
annoView.annotation = annotation; // 4.设置图片
annoView.image = [UIImage imageNamed:annotation.icon]; return annoView;
} #pragma mark 点击了大头针
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
// 0.假设是系统自带的大头针,则直接返回...
if (![view.annotation isKindOfClass:[MyAnnotation class]]) {
return;
} // 1.调用父类的方法,展示详情控制器,并提供数据源
MyAnnotation *anno = view.annotation;
[self showDetail:anno.deal]; // 2.让选中的大头针居中(成为中心点)
[mapView setCenterCoordinate:anno.coordinate animated:YES]; // 3.让view周边产生一些阴影效果
view.layer.shadowColor = [UIColor blueColor].CGColor;
view.layer.shadowOpacity = 1;
view.layer.shadowRadius = 10;
} #pragma mark - 监听到定位城市发生改变时,又一次刷新webView
// 0.监听定位完毕的通知
- (void)dataChange
{
// 1.城市对象
City *city = [MetaDataTool sharedMetaDataTool].currentCity;
CLGeocoder *geo = [[CLGeocoder alloc] init];
// 全球搜索 某某城市
[geo geocodeAddressString:city.name completionHandler:^(NSArray *placemarks, NSError *error) { // 定位,解析完毕之后,就能够提供经纬度
CLPlacemark *place = placemarks[0];
// 1.用户位置的(中心点)
CLLocationCoordinate2D center = place.location.coordinate;
city.position = place.location.coordinate;
// 重要,将城市用工具记住,由于发送请求时,还用到了城市 名和 经纬度
[LocationTool sharedLocationTool].locationCity = city;
// 2.确定好中心点之后,再确定跨度(范围)
// 3.依据中心点和跨度之后,就确定好了区域
MKCoordinateRegion region = MKCoordinateRegionMake(center, kSpan);
// 4.让mapView显示到指定的区域
[_mapView setRegion:region animated:YES];
}];
}
@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. AdaBoostClassifier实战

    AdaBoostClassifier实战 部分内容摘自:http://blog.csdn.net/sun_shengyun/article/details/54289955 这里我们用一个具体的例子来 ...

  2. 杂项:Kafka

    ylbtech-杂项:Kafka Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站 ...

  3. 【TYVJ1936】【Clover杯NOIP模拟赛IV】太空战队

    [问题描述] 在社会经济高速发展的今天,借助高科技手段,组建太空战队的愿望就快实现了. 战队属下有N个航天员.作为空军选拔上来的佼佼者,每个航天员都有与生俱来的骄傲——他们每个人都认为自己是最强的或者 ...

  4. .net core发布到IIS后502.5错误

    net core 在win7系统发布后,出现在502.5错误. 打开“开始”菜单,搜索“事件查看器”,然后选择“事件查看器”应用. 在“事件查看器”中,打开“Windows 日志”节点. 选择“应用程 ...

  5. delphi 用idhttp做web页面数据抓取 注意事项

    这里不讨论webbrowse方式了 .直接采用indy的 idhttp  Get post 可以很方便的获取网页数据. 但如果要抓取大量数据 程序稳定运行不崩溃就不那么容易了.这几年也做了不少类似工具 ...

  6. 基于jQuery封装一个瀑布流插件

    /*封装一个瀑布流插件*/ (function($){ $.fn.WaterFall = function(){ /*这是你初始化 调用这个方法的时候的 那个jquery选着到的dom对象 this* ...

  7. XML DTD跟SCHEMA约束 语法了解

    dtd语法 元素: <!Element 元素名称 数据类型|包含内容> 数据类型: #PCDATA:普通文本 使用的时候一般用()引起来 包含内容: 该元素下可以出现哪些元素, 用()引起 ...

  8. Walking on the path of Redis --- Redis configuration

    废话开篇 Redis的安装是非常简单易操作的,但是配置就有点复杂了,要想得到高性能的Redis数据服务,深入了解下如何配置是很重要的. 配置详解 下面是主要的参数及说明,至于如何配置才能最优,目前还不 ...

  9. String,StringBuffer,StringBuilder效率优先关系说明

    String,StringBuffer,StringBuilder效率优先关系说明: public class StringBufferWithStringBuilder { public stati ...

  10. css 添加阴影

    添加阴影,分为内阴影和外阴影. inset:内阴影. 不写默认外阴影. box-shadow: 水平位移  垂直位移  模糊半径 #box-shadow{ -moz-box-shadow:5px 5p ...