代码如下:

//
//  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. ASP.NET MVC3 在_ViewStart設定Layout後用RenderAction的注意事項

    ASP.NET MVC3 在_ViewStart設定Layout後用RenderAction的注意事項 3/24 TW MVC第一次活動圓滿的結束了,雖然是RC,但也來了不少願意聽我們分享的好朋友. ...

  2. Robot Framework自动化测试(一)---第一个脚本

    最近工具中用Robot Framework框架来做自动化,所以,花时间学习了一下. =======所需环境=================== Python: https://www.python. ...

  3. SQL Server里因丢失索引造成的死锁

    在今天的文章里我想演示下SQL Server里在表上丢失索引如何引起死锁(deadlock)的.为了准备测试场景,下列代码会创建2个表,然后2个表都插入4条记录. -- Create a table ...

  4. EFcodeFirst+T4=操纵任意数据库

    之前有写过两篇,EF选择Mysql数据源 跟 EF添加ADO.NET实体模型处直接选择Oracle数据源,其方便之处就不多说了,使用DBfirst直接点点点就能与数据库双向更新,而且关键是方便我们使用 ...

  5. 1117 新冲刺 day1

    项目需求确定 现阶段我们进行的项目是到店点餐系统.主要是开发手机端app为用户提供方便快捷的点餐服务.免去顾客到店后遇到因吃饭的人太多而找不到服务人员点餐的窘境.减少了服务人员因为忙碌而导致下单慢的问 ...

  6. 11.22 点餐APP第一阶段总结

    第一个冲刺结束了,任务也算是完成了. 团队合作不像单独做那样想怎么来就怎么来,各个人都不同的意见,最后方案的需要每个人一致同意通过才能执行. 不过团队合作分配到每个人的任务也相对轻一点,而且遇到问题解 ...

  7. Winform开发框架之统计图表的实现

    在前面的一些随笔中,介绍了不少我的Winform框架的特性,上篇随笔<Winform开发框架之通用高级查询模块>对其中的通用高级模块进了一个整理说明,本篇继续介绍Winform开发框架重要 ...

  8. Web开发者选择的最佳HTML5/CSS3代码生成器

    原文地址:http://codecloud.net/css3-code-generators-for-web-programmers-6672.htmlHTML5 和CSS3是一入门就能用的最好的语言 ...

  9. csharp: MongoDB

    安装配置: Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-o ...

  10. 那些教程没有的php2-对象

    php.net 对象 在类定义内部,可以用 new self 和 new parent 创建新对象. 当把一个对象已经创建的实例赋给一个新变量时,新变量会访问同一个实例,就和用该对象赋值一样.可以用克 ...