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#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...
随机推荐
- (转) 寄存器、RAM、ROM、Flash相关概念区别整理
转自 http://m.blog.chinaunix.net/uid-30077524-id-5570244.html 文章对这几个东西讲得很清楚,值得收藏. 寄存器 寄存器是中央处理器内的组成部份. ...
- css的两种引用方式 link和@import
学习web开发的最大乐趣就是不断的发现自己以前不曾见过的东西,这些东西对于我来说是那么的新鲜有趣. 比如说今天偶尔研究别人的网站,就发现了有趣的东东. 当点开此网页的css时(这个css文件命名方式就 ...
- Lua中调用C函数
Lua利用一个虚拟的堆栈来给C传递值或从C获取值.每当Lua调用C函数,都会获得一个新的堆栈,该堆栈初始包含所有的调用C函数所需要的参数值(Lua传给C函数的调用实参),并且C函数执行完毕后,会把返回 ...
- Tools - Notepad++
NotePad++ https://notepad-plus-plus.org/ 修改主题 依次点击设置---语言格式设置---选择主题,在显示界面中修改相关设置(背景色.前景色.字体等). 双文本横 ...
- 『创意欣赏』20款精致的 iOS7 APP 图标设计
这篇文章给大家分享20款精致的 iOS7 移动应用程序图标,遵循图形设计的现代潮流,所有图标都非常了不起,给人惊喜.通过学习这些移动应用程序图标,设计人员可以提高他们的创作,使移动用户界面看起来更有趣 ...
- [阅读]个人阅读作业week7
People-oriented in Agile People-oriented in Agile One Leader Prepare Good ideas from users People-or ...
- Linq专题之Lambda表达式
这一节我们讲的Lambda表达式跟匿名函数有关.Lambda表达式就是一个匿名函数,它可以包含表达式和语句,并且可以创建委托和表达式树. Lambda表达式的组成: 输入参数.Lambda运算符(=& ...
- C#调用webservice 时如何传递实体对象
在webservice端公开一个实体类,然后实例化,赋值,然后再给到webservice,可以实现,但是,即使调用端和service端的实体类完全一致,你也要重新实例化service端的,重新赋值,将 ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake
Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping ...
- KMP---Count the string
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/A Description It is well k ...