IOS uses CoreLocation and MapKit to develop and search for nearby mall functions, corelocationmapkit

Source: Internet
Author: User

IOS uses CoreLocation and MapKit to develop and search for nearby mall functions, corelocationmapkit

The Code is as follows:

//
// 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 = @ "Search for malls nearby ";
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: callback]; topSearchButton. frame = CGRectMake (0, 0, 50, 50); [topSearchButton setTitle: @ "Search" forState: UIControlStateNormal]; [topSearchButton addTarget: self action: @ selector (searchNearbyShopAction :) forControlEvents: UIControlEvent TouchUpInside]; 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 ([CLLocati OnManager locationServicesEnabled]) {NSLog (@ "enabled"); [_ locationManager startUpdatingLocation]; self. locationManager. distanceFilter = kCLDistanceFilterNone;} else {NSLog (@ "the positioning function cannot be enabled") ;}- (void) searchNearbyShopWithRegion: (MKCoordinateRegion) region {// obtain nearby information self. nearbyInfoArray = [NSMutableArray array]; MKLocalSearchRequest * request = [[MKLocalSearchRequest alloc] init]; request. region = Region; request. principal = @ "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 (@ "Search error, % @", error) ;}}] ;}# pragma mark-IBActions-(void) searchNearbyShopAction: (UIButton *) searchButton {NSLog (@ "re-search for nearby malls"); 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 (@ "latitude = % f, precision = % f ", location. coordinate. latitude, location. coordinate. longpolling); //} # 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 (@ "succeeded in obtaining the geographic location name =%@, locality =%@", placeMark. name, placeMark. locality); userLocation. title = placeMark. name; userLocation. subtitle = placeMark. locality;}]; // The current location is displayed in the map CLLocationCoordinate2D center = userLocation. location. coordinate; MKCoordinateSpan span = MKCoordinateSpanMake (0.009310, 0.007812); MKCoordinateRegion region = centers, span); MKCoordinateRegion searchRegion = region (userLocation. coordinate, KSearchAreaMeters, KSearchAreaMeters); currentRegion = searchRegion; [self. mapView setRegion: region animated: YES];} @ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.