這里只貼主要代碼 詳情請(qǐng)下載Demo
1.首先在AppDelegate.m文件添加熟悉的Key
[AMapServices sharedServices].apiKey = @"XXX"; ?(ps:此處打上馬賽克-_-)
2.定位自己
#pragma mark - Location
- (void)updateCurrentLocation
{
//一次獲取定位信息(帶反編碼)
[XYQProgressHUD showMessage:@"正在定位"];
// 帶逆地理(返回坐標(biāo)和地址信息)颤殴。將下面代碼中的 YES 改成 NO ,則不會(huì)返回地址信息鼻忠。
[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
[XYQProgressHUD hideHUD];
if (error)
{
[XYQProgressHUD showError:@"定位失敗"];
NSLog(@"error:%@", error);
return;
}
//定位信息
//? ? ? ? NSLog(@"location:%@", location);
//逆地理信息
if (regeocode)
{
NSLog(@"reGeocode:%@", regeocode);
}
[XYQProgressHUD showSuccess:@"定位成功"];
_curLocation = location;
_annotation = [[CurrentLocationAnnotation alloc] init];
_annotation.coordinate = _curLocation.coordinate;
_annotation.title = regeocode.formattedAddress;
[self.mapView addAnnotation:_annotation];
[self.mapView selectAnnotation:_annotation animated:YES];
// 設(shè)置地圖中心點(diǎn)為用戶位置
[_mapView setCenterCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) animated:YES];
//讓地圖在縮放過程中移到當(dāng)前位置試圖
[_mapView setZoomLevel:17 animated:YES];
// 周邊檢索[self searchPoiWithCenterCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)];
}];
}
/* 根據(jù)中心點(diǎn)坐標(biāo)來搜周邊的POI. */
- (void)searchPoiWithCenterCoordinate:(CLLocationCoordinate2D )coord
{
AMapPOIAroundSearchRequest*request = [[AMapPOIAroundSearchRequest alloc] init];
request.location = [AMapGeoPoint locationWithLatitude:coord.latitude? longitude:coord.longitude];
request.radius? = 500;? ? ? ? ? ? /// 搜索范圍
request.types = @"餐飲";
request.sortrule = 1;? ? ? ? ? ? ? ///排序規(guī)則
[self.search AMapPOIAroundSearch:request];
}
/* POI 搜索回調(diào). */
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
if (response.pois.count == 0)
{
return;
}
//解析response獲取POI信息涵但,具體解析見 Demo
[self.mapView removeAnnotations:_poiAnnotations];
[_poiAnnotations removeAllObjects];
[response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
[annotation setCoordinate:CLLocationCoordinate2DMake(obj.location.latitude, obj.location.longitude)];
[annotation setTitle:[NSString stringWithFormat:@"%@ - %ld米", obj.name, (long)obj.distance]];
[annotation setSubtitle:obj.address];
[_poiAnnotations addObject:annotation];
}];
[self showPOIAnnotations];
}
// 設(shè)置地圖使其可以顯示數(shù)組中所有的annotation, 如果數(shù)組中只有一個(gè)則直接設(shè)置地圖中心為annotation的位置。
- (void)showPOIAnnotations
{
[self.mapView addAnnotations:_poiAnnotations];
if (_poiAnnotations.count == 1)
{
self.mapView.centerCoordinate = [(MAPointAnnotation *)_poiAnnotations[0] coordinate];
[self.mapView setZoomLevel:16 animated:NO];
}
}
?Demo 鏈接:https://github.com/kangtian/My-capsule