代码如下:

//
//  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];      }

@end

iOS 利用CoreLocation和MapKit开发搜索附近的商场功能的更多相关文章

  1. (IOS)CoreLocation 和 MapKit 的应用

    CoreLocation是控制GPS硬件获取地理坐标信息的系统类库,而MapKit是系统地图的工具包,将两者结合使用可以实现不同的地图功能. 1.CoreLocation 在CoreLocation中 ...

  2. 【高德API】如何利用MapKit开发全英文检索的iOS地图

    原文:[高德API]如何利用MapKit开发全英文检索的iOS地图 制作全英文地图的展示并不困难,但是要制作全英文的数据检索列表,全英文的信息窗口,你就没办法了吧.告诉你,我有妙招!使用iOS自带的M ...

  3. [iOS 利用MapKit和CoreLocation框架打造精简的定位和导航]

    运行效果:            一.利用<CoreLocation/CoreLocation.h>定位 创建变量 CLLocationManager *locationManager , ...

  4. 利用WordPress REST API 开发微信小程序从入门到放弃

    自从我发布并开源WordPress版微信小程序以来,很多WordPress网站的站长问有关程序开发的问题,其实在文章:<用微信小程序连接WordPress网站>讲述过一些基本的要点,不过仍 ...

  5. 《iOS开发全然上手——使用iOS 7和Xcode 5开发移动与平板应用》之Objective-C新手训练营

    编写Hello World应用程序通常被觉得,是学习不论什么编程语言的第一步.在这一章,你将创建iOS版的Hello World应用程序作为起步,高速了解Xcode这个开发iOS应用程序的主要工具. ...

  6. 转载:如何利用Vim进行Erlang开发

    转自:http://ovalpo.info/how_to_use_vim_for_erlang_dev/ 如何利用Vim进行Erlang开发 by Martin J. Logan on Septemb ...

  7. Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面

    Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...

  8. iOS学习笔记-地图MapKit入门

    代码地址如下:http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错漏 ...

  9. C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi

    C#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...

随机推荐

  1. 【原创】C#搭建足球赛事资料库与预测平台(2) 数据库与XCode组件

            本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源C#彩票数据资料库系列文章总目录:[目录]C#搭建足球赛事资料库与预测平台与彩票数据分析目录  本篇文章开始将逐步 ...

  2. Pass云Docker介绍

    1.Docker 简介 Docker是一个开源可以将任何应用包装在”LXC容器”中运行的工具.如果说VMware,KVM包装的虚拟机,Docker包装的是应用.是一个实至名归的PaaS. 当应用被打包 ...

  3. 你不一定知道的几个很有用的 Git 命令

    这里给大家分享一些很有用的 Git 命令,其中很多用法你可能都不知道,无论你是工作在团队环境中或在您的个人项目中,这些命令将对你帮助很大,让你可以更加高效的进行项目开发,更轻松愉快的工作和生活. 您可 ...

  4. HTTP状态码大全(转自wiki)

    1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非在某些试 ...

  5. Android学习笔记之HttpClient实现Http请求....

    PS:最近光忙着考试了....破组成原理都看吐了....搞的什么也不想干...写篇博客爽爽吧....貌似明天就考试了...sad... 学习笔记: 1.如何实现Http请求来实现通信.... 2.解决 ...

  6. MyBatis魔法堂:各数据库的批量Update操作

    一.前言   MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...

  7. python反转字符串(简单方法)及简单的文件操作示例

    Python反转字符串的最简单方法是用切片: >>> a=' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号之前)表示切片 ...

  8. CSS布局 -- 左右定宽,中间自适应

    左右定宽,中间自适应 有几种方法可以实现 改变窗口大小看看? 方案一: 左右设置绝对定位,定宽,中间设置margin-left  margin-right 查看 demo <!DOCTYPE h ...

  9. Brute Force & STL --- UVA 146 ID Codes

     ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...

  10. (Python学习4)List对象

    1.PyListObject对象 typedef struct { PyObject_VAR_HEAD PyObject **ob_item; Py_ssize_t allocated; } PyLi ...