本文主要实现如图功能
- 配置就不用多说,看使用高德正确姿势(一)
直奔主题
1.实现基础定位在地图上显示
//添加头文件
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
//搜索基础类
#import <AMapSearchKit/AMapSearchKit.h>
//viewDidLoad
///初始化地图
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
///把地图添加至view
[self.view addSubview:_mapView];
///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollow;
//设置地图缩放比例,即显示区域
[_mapView setZoomLevel:15.1 animated:YES];
_mapView.delegate = self;
//设置定位精度
_mapView.desiredAccuracy = kCLLocationAccuracyBest;
//设置定位距离
_mapView.distanceFilter = 5.0f;
2.实现点击小蓝点显示当前位置的具体地址
2.1 //初始化_search 用于逆编码
self.search =[[AMapSearchAPI alloc] init];
self.search.delegate=self;
//把中心点设成自己的坐标
_mapView.centerCoordinate = self.currentLocation.coordinate;
2.1实现代理
#pragma mark 定位更新回调
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
if(updatingLocation)
{
self.currentLocation = [userLocation.location copy];
[_mapView setCenterCoordinate:CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude) animated:YES];
}
}
-(void)setCurrentLocation:(CLLocation *)currentLocation{
if (!_currentLocation) {
_currentLocation = currentLocation;
_mapView.centerCoordinate = currentLocation.coordinate;
[self reGeoCoding];
}
}
#pragma mark 逆地理编码,经纬度编码成地址
-(void)reGeoCoding{
if (_currentLocation) {
AMapReGeocodeSearchRequest *request =[[AMapReGeocodeSearchRequest alloc] init];
request.location =[AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
[_search AMapReGoecodeSearch:request];
}
}
#pragma mark 搜索请求发起后的回调,用于标记自己当前的位置
/**失败回调*/
-(void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
NSLog(@"request: %@------error: %@",request,error);
}
//*成功回调
-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
//我们把编码后的地理位置,显示到 大头针的标题和子标题上
NSString *title =response.regeocode.addressComponent.city;
NSLog(@"%@",title);
if (title.length == 0) {
title = response.regeocode.addressComponent.province;
}
// NSLog(@"=====%@",request.location);
if (request.location.latitude == _currentLocation.coordinate.latitude&&request.location.longitude == _currentLocation.coordinate.longitude) {
_mapView.userLocation.title = title;
_mapView.userLocation.subtitle = response.regeocode.formattedAddress;
}else{
self.anomationPoint.title = title;
self.anomationPoint.subtitle = response.regeocode.formattedAddress;
}
}
网友评论