#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];
}
}];
}