iOS 自带地图实现附近查询搜索


#pragma mark -------

#pragma mark 地理位置相关

- (void)locan

{    //定位管理器   

_locationManager=[[CLLocationManager alloc]init];     

  if (![CLLocationManager locationServicesEnabled])

  {      

[SVProgressHUD showErrorWithStatus:@"定位服务当前可能尚未打开,请设置打开!"];        return; 

  }   

//如果没有授权则请求用户授权   

if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined)

{    

  [_locationManager requestWhenInUseAuthorization]; 

  } 

  else

    {

      //设置代理    

  _locationManager.delegate=self; 

      //设置定位精度        _locationManager.desiredAccuracy=kCLLocationAccuracyBest;        [_locationManager requestAlwaysAuthorization];     

  //启动跟踪定位  

    [_locationManager startUpdatingLocation]; 

  }

  }

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations


  CLLocation *currLocation = [locations lastObject];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

  [_locationManager stopUpdatingLocation];

  [geocoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {

if(error)

{

HUDShowError(@"查询失败");

} else if(placemarks && placemarks.count > 0)

{

[self issueLocalSearchLookup:@"ATM" usingPlacemarksArray:placemarks];

}

}];

}

- (void)locationManager:(CLLocationManager *)manager

didFailWithError:(NSError *)error;

{

HUDShowError(@"定位失败");

}


-(void)issueLocalSearchLookup:(NSString *)searchString usingPlacemarksArray:(NSArray *)placemarks {

// Search 0.250km from point for stores.

CLPlacemark *placemark = placemarks[0];

CLLocation *location = placemark.location;

self.coords = location.coordinate;

// Set the size (local/span) of the region (address, w/e) we want to get search results for.

MKCoordinateSpan span = MKCoordinateSpanMake(0.6250, 0.6250);

MKCoordinateRegion region = MKCoordinateRegionMake(self.coords, span);

[self.mapView.mapView setRegion:region animated:NO];

// Create the search request

self.localSearchRequest = [[MKLocalSearchRequest alloc] init];

self.localSearchRequest.region = region;

self.localSearchRequest.naturalLanguageQuery = searchString;

// Perform the search request...

self.localSearch = [[MKLocalSearch alloc] initWithRequest:self.localSearchRequest];

[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

if(error){

HUDShowError(@"查询无结果")

return;

} else {

// We are here because we have data!  Yay..  a whole 10 records of it too *flex*

// Do whatever with it here...

for(MKMapItem *mapItem in response.mapItems){

// Show pins, pix, w/e...

NSLog(@"Name for result: = %@", mapItem.name);

// Other properties includes: phoneNumber, placemark, url, etc.

// More info here: https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKLocalSearch/Reference/Reference.html#//apple_ref/doc/uid/TP40012893

}

MKCoordinateSpan span = MKCoordinateSpanMake(0.2, 0.2);

MKCoordinateRegion region = MKCoordinateRegionMake(self.coords, span);

[self.mapView.mapView setRegion:region animated:NO];

}

}];

}

你可能感兴趣的:(iOS 自带地图实现附近查询搜索)