这里只贴主要代码 详情请下载Demo
1.首先在AppDelegate.m文件添加熟悉的Key
[AMapServices sharedServices].apiKey = @"XXX"; (ps:此处打上马赛克-_-)
2.定位自己
#pragma mark - Location
- (void)updateCurrentLocation
{
//一次获取定位信息(带反编码)
[XYQProgressHUD showMessage:@"正在定位"];
// 带逆地理(返回坐标和地址信息)。将下面代码中的 YES 改成 NO ,则不会返回地址信息。
[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];
// 设置地图中心点为用户位置
[_mapView setCenterCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) animated:YES];
//让地图在缩放过程中移到当前位置试图
[_mapView setZoomLevel:17 animated:YES];
// 周边检索[self searchPoiWithCenterCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)];
}];
}
/* 根据中心点坐标来搜周边的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; ///排序规则
[self.search AMapPOIAroundSearch:request];
}
/* POI 搜索回调. */
- (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];
}
// 设置地图使其可以显示数组中所有的annotation, 如果数组中只有一个则直接设置地图中心为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