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#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...
随机推荐
- 笔记——Visual Studio 程序员箴言
记录了一些感觉比较用得到的tips用于随时查看.要想看完整的的内容还是阅读<Visual Studio 程序员箴言>,不过有些内容我在新版本的VS里没能实现,或者有替代方案了. 避免意外复 ...
- Tools - Notepad++
NotePad++ https://notepad-plus-plus.org/ 修改主题 依次点击设置---语言格式设置---选择主题,在显示界面中修改相关设置(背景色.前景色.字体等). 双文本横 ...
- Android OpenGL 基础入门
Android 自从2.2 版本之后之后开始支持OpenGL,在没有支持OpenGL 的 GPU的情况下,也可以使用(通过软件来模拟).在Android上使用Opengl操作的对象是GLSurface ...
- HTML简明教程(一)
HTML简明教程(一) 内容主体来自:W3School 一.HTML 简介 二.HTML 基础 三.HTML 元素 四.HTML 属性 五.HTML 标题 六.HTML 段落 七.HTML 文本格式化 ...
- Scrum团队
5.Scrum团队成立 5.1 团队名称,团队目标.团队口号.团队照: 团队名称:@four! 团队目标:做出像“数学口袋精灵”那么棒的软件 团队口号:多劳多得 团队照: 5.2 角色分配 产品负责人 ...
- eclipse中去掉Js/javsscript报错信息
1.首先在problem>errors中删除所有js错误: 如下图 2.然后再勾选掉javascript Validator: 3.clean下项目吧,你会发现再也不出现js红叉叉了,哈哈.
- PP66 EEPPPPMM SSyysstteemm AAddmmiinniissttrraattiioonn GGuuiiddee 16 R1
※★◆●PP66 EEPPPPMM SSyysstteemm AAddmmiinniissttrraattiioonn GGuuiiddee 16 R1AApprriill 22001166Conte ...
- 正则表达式相关:C# 抓取网页类(获取网页中所有信息)
类的代码: using System; using System.Data; using System.Configuration; using System.Net; using System.IO ...
- Unity3D脚本语言UnityScript初探
译者注: Unity3D中支持三种语言:JavaScript.C#.Boo,很多人不知道如何选择,通过这篇译文,我们可以搞清楚这三者语言的来龙去脉,对选择主语言有一定的借鉴意义. 首先,Unity是基 ...
- hibernate3 Duplicate class/entity mapping(异常)
hibernate3 Duplicate class/entity mapping(异常) 代码: Configuration config = new Configuration().ad ...