iOS 自帶地圖實(shí)現(xiàn)附近查詢搜索


#pragma mark -------

#pragma mark 地理位置相關(guān)

- (void)locan

{? ? //定位管理器? ?

_locationManager=[[CLLocationManager alloc]init];? ? ?

? if (![CLLocationManager locationServicesEnabled])

? {? ? ??

[SVProgressHUD showErrorWithStatus:@"定位服務(wù)當(dāng)前可能尚未打開,請(qǐng)?jiān)O(shè)置打開!"];? ? ? ? return;?

? }? ?

//如果沒有授權(quán)則請(qǐng)求用戶授權(quán)? ?

if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined)

{? ??

? [_locationManager requestWhenInUseAuthorization];?

? }?

? else

? ? {

? ? ? //設(shè)置代理? ??

? _locationManager.delegate=self;?

? ? ? //設(shè)置定位精度? ? ? ? _locationManager.desiredAccuracy=kCLLocationAccuracyBest;? ? ? ? [_locationManager requestAlwaysAuthorization];? ? ?

? //啟動(dòng)跟蹤定位??

? ? [_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(@"查詢無結(jié)果")

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

}

}];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容