iOS 利用CoreLocation和MapKit开发搜索附近的商场功能
代码如下:
// SearchNearbyShopViewController.m
// SearchNearbyShop
//
// Created by Linzhixiao on 16/2/14.
// Copyright © 2016年 B5m. All rights reserved.
//
#import "SearchNearbyShopViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#define KSearchAreaMeters 100
@interface SearchNearbyShopViewController () <CLLocationManagerDelegate,MKMapViewDelegate>
{
MKCoordinateRegion currentRegion;
}
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLGeocoder *geocoder;
@property (nonatomic,strong) NSMutableArray *nearbyInfoArray;
@end
@implementation SearchNearbyShopViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"搜索附近商场";
self.view.backgroundColor = [UIColor whiteColor];
[self configNavigation];
[self locationManager];
[self requestLocationAutoorize];
[self GetCurrentLocation];
[self geocoder];
[self.view addSubview:self.mapView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Custom Accessors
- (CLLocationManager *)locationManager {
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
}
return _locationManager;
}
- (MKMapView *)mapView {
if (!_mapView) {
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.mapType = MKMapTypeStandard;
_mapView.userTrackingMode = MKUserTrackingModeFollow;
_mapView.delegate = self;
}
return _mapView;
}
- (CLGeocoder *)geocoder {
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init]; } return _geocoder;}#pragma mark - Private- (void)configNavigation { UIButton *topSearchButton = [UIButton buttonWithType:UIButtonTypeSystem]; topSearchButton.frame = CGRectMake(0, 0, 50, 50); [topSearchButton setTitle:@"搜索" forState:UIControlStateNormal]; [topSearchButton addTarget:self action:@selector(searchNearbyShopAction:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:topSearchButton]; self.navigationItem.rightBarButtonItem = rightButtonItem;}- (void)requestLocationAutoorize { CGFloat systemVersion = [[UIDevice currentDevice].systemVersion floatValue]; if (systemVersion >= 8) { [_locationManager requestAlwaysAuthorization]; }}- (void)GetCurrentLocation { if ([CLLocationManager locationServicesEnabled]) { NSLog(@"定位服务已开启"); [_locationManager startUpdatingLocation]; self.locationManager.distanceFilter = kCLDistanceFilterNone; }else { NSLog(@"定位功能不能开启"); } }- (void)searchNearbyShopWithRegion: (MKCoordinateRegion )region { // 获得附近的信息 self.nearbyInfoArray = [NSMutableArray array]; MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; request.region = region; request.naturalLanguageQuery = @"school"; MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request]; [localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) { if (!error) { [self.nearbyInfoArray addObjectsFromArray:response.mapItems]; for (MKMapItem *item in _nearbyInfoArray) { NSLog(@"name = %@, ",item.name); } }else { NSLog(@"搜索错误, %@",error); } }];}#pragma mark - IBActions- (void)searchNearbyShopAction: (UIButton *)searchButton { NSLog(@"重新搜索附近商场"); if (currentRegion.span.latitudeDelta == 0.0) { return; }else { [self searchNearbyShopWithRegion:currentRegion]; }}#pragma mark - CLLocationManagerDelegate//- (void)locationManager:(CLLocationManager *)manager// didUpdateLocations:(NSArray<CLLocation *> *)locations {// CLLocation *location = [locations firstObject];// NSLog(@"纬度=%f, 精度=%f",location.coordinate.latitude,location.coordinate.longitude);//}#pragma mark - MKMapViewDelegate- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *placeMark = [placemarks firstObject]; NSLog(@"获取地理位置成功 name = %@, locality = %@",placeMark.name,placeMark.locality); userLocation.title = placeMark.name; userLocation.subtitle = placeMark.locality; }]; // 当前位置显示到地图 CLLocationCoordinate2D center = userLocation.location.coordinate; MKCoordinateSpan span = MKCoordinateSpanMake(0.009310, 0.007812); MKCoordinateRegion region = MKCoordinateRegionMake(center, span); MKCoordinateRegion searchRegion = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, KSearchAreaMeters,KSearchAreaMeters); currentRegion = searchRegion; [self.mapView setRegion:region animated:YES]; }
iOS 利用CoreLocation和MapKit开发搜索附近的商场功能的更多相关文章
- (IOS)CoreLocation 和 MapKit 的应用
CoreLocation是控制GPS硬件获取地理坐标信息的系统类库,而MapKit是系统地图的工具包,将两者结合使用可以实现不同的地图功能. 1.CoreLocation 在CoreLocation中 ...
- 【高德API】如何利用MapKit开发全英文检索的iOS地图
原文:[高德API]如何利用MapKit开发全英文检索的iOS地图 制作全英文地图的展示并不困难,但是要制作全英文的数据检索列表,全英文的信息窗口,你就没办法了吧.告诉你,我有妙招!使用iOS自带的M ...
- [iOS 利用MapKit和CoreLocation框架打造精简的定位和导航]
运行效果: 一.利用<CoreLocation/CoreLocation.h>定位 创建变量 CLLocationManager *locationManager , ...
- 利用WordPress REST API 开发微信小程序从入门到放弃
自从我发布并开源WordPress版微信小程序以来,很多WordPress网站的站长问有关程序开发的问题,其实在文章:<用微信小程序连接WordPress网站>讲述过一些基本的要点,不过仍 ...
- 《iOS开发全然上手——使用iOS 7和Xcode 5开发移动与平板应用》之Objective-C新手训练营
编写Hello World应用程序通常被觉得,是学习不论什么编程语言的第一步.在这一章,你将创建iOS版的Hello World应用程序作为起步,高速了解Xcode这个开发iOS应用程序的主要工具. ...
- 转载:如何利用Vim进行Erlang开发
转自:http://ovalpo.info/how_to_use_vim_for_erlang_dev/ 如何利用Vim进行Erlang开发 by Martin J. Logan on Septemb ...
- Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面
Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...
- iOS学习笔记-地图MapKit入门
代码地址如下:http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错漏 ...
- C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi
C#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...
随机推荐
- @media 手机与IPAD与PC
@media screen and (min-width: 769px) { bindCard{height:28.5em} } @media screen and (min-device-width ...
- jquery.uploadify 异常 “__flash__removeCallback”未定义
使用场景结合artdialog弹出框使用时发生“__flash__removeCallback”未定义,原因在于artdialog基于iframe加载的uloadify,在关闭artdialog的时候 ...
- 移动审批App开发总结
公司新需求要在手机上进行审批. 现在开发完成了. 总结:1.初步把公司的工作流模块做成RPC服务,公共服务可以进行调用. 2.服务分层,每个App的页面对应一个服务端的接口,作为前端控制器,用来从更低 ...
- iOS-UIButton-设置button标题和图片位置
一.效果图 1.Button被点击之前 2.Button被点击之后 二.代码 - (void)createBtn3 { UIImage * buttonImage = [UIImage imageNa ...
- Android学习笔记之DocumentBuilder的使用....
PS:当你的才华还撑不起你的野心时,那你需要静下心来学习..... 学习内容: 1.从服务器上获取XML文档... 2.解析XML文档中的内容... XML文件想必大家都非常的熟悉,可扩展的标记语 ...
- Travis-CI与Latex构建开源中文PDF
博主有一本开源书籍,用 latex 排版,托管在Github上.但用 latex 不像是 Markdown,当tex文本更新时,用于最终浏览的PDF文件很难得到及时的更新, 所以博主一直想找到一套工具 ...
- Sprint第三个冲刺(第五天)
一.Sprint介绍 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图:
- 数论 - 高精度Fibonacci数 --- UVa 10183 : How Many Fibs ?
How many Fibs? Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := f n ...
- RCU-数据库初始化参数
C:\Windows\System32>sqlplus sys/As123456 as sysdba SQL> show parameter processes; SQL> shut ...
- Hyperion Business Modeling for Microsoft Windows (32-bit)
介质包搜索 常见问题 说明 复查 许可证列表 以确定需要下载的产品程序包. 请选择产品程序包和平台,然后单击“查找”. 如果只有一项结果,则可以看到下载页.如果有多个结果,请选择一个,然后单 ...