iOS 利用CoreLocation和MapKit開發搜尋附近的商場功能,corelocationmapkit

來源:互聯網
上載者:User

iOS 利用CoreLocation和MapKit開發搜尋附近的商場功能,corelocationmapkit

代碼如下:

//
//  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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.