代码如下:
//
// 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]; }
// 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