高德地图API文档:
这里先说定位(在地图中显示用户位置,获取经纬度),逆地理编码
值得注意的是,高德地图的mapview对象不能多次创建,最好是使用单实例.不然程序很容易会崩
第一步:配置环境
1>.到http://lbs.amap.com/api/ios-sdk/down/ 上下载SDK(本文都是使用2D的SDK,3D的请自己查看.)
2>.注册账号,获得appkey http://id.amap.com/?type=spa&ref=http://lbs.amap.com/
3>.添加系统库 http://lbs.amap.com/api/ios-sdk/guide/project/
4>.在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。
第二步:开始编程了
1>.首先要在appDeletage中写上key :
[MAMapServices sharedServices].apiKey = @"5aed7f0e8121d1d985e3344f98ca5955";
2>.在viewDidLoad中实例化一个mapview对象(这点很重要,在官方demo中将这一句放在外面了很难找出来)
self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
self.mapView.frame = self.view.bounds;
self.mapView.delegate = self;//设置代理也重要
[self.view addSubview:self.mapView];
实例化地图实例以后:
self.mapView.showsUserLocation = YES;//这句就是开启定位
self.mapView.userTrackingMode = MAUserTrackingModeFollow; // 追踪用户位置.
开始定位以后,MAUserLocation * userLocat = self.mapView.userLocation;
就可以通过地图实例获得位置了.如:
CGFloat lat = self.mapView.userLocation.coordinate.latitude;
要实时获得用户的经纬度:则需要添加下面这个代理方法
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
if (updatingLocation) {
NSLog(@"latitude : %f , longitude : %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
}
}
3>初始化完地图实例,还要弄个搜索实例用于反编码:最好也是在viewDidLoad中搞啦
self.search = [[AMapSearchAPI alloc] initWithSearchKey:[MAMapServices sharedServices].apiKey Delegate:self];
官方demo中delegate写了个nil上去,直接copy了.组长突然过来看进度;我擦,害我被组长叼了一顿;
4>开始反编码
MAUserLocation * userLocat = self.mapView.userLocation;
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:userLocat.coordinate.latitude longitude:userLocat.coordinate.longitude];
regeo.requireExtension = YES;
//发起逆地理编码
[self.search AMapReGoecodeSearch:regeo];
弄完发请求之后,还要搞个delegate的回调.
/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (response.regeocode != nil)
{
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);
//添加一根针
ReGeocodeAnnotation *reGeocodeAnnotation = [[ReGeocodeAnnotation alloc] initWithCoordinate:coordinate
reGeocode:response.regeocode];
[self.mapView addAnnotation:reGeocodeAnnotation];//要添加标注
[self.mapView selectAnnotation:reGeocodeAnnotation animated:YES];//标注是否有动画效果
}
}
如果要添加标注:那么就要实现下面这个方法
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[ReGeocodeAnnotation class]])
{
static NSString *invertGeoIdentifier = @"invertGeoIdentifier";
MANaviAnnotationView *poiAnnotationView = (MANaviAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:invertGeoIdentifier];
if (poiAnnotationView == nil)
{
poiAnnotationView = [[MANaviAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:invertGeoIdentifier];
}
poiAnnotationView.animatesDrop = YES;
poiAnnotationView.canShowCallout = YES;
//show detail by right callout accessory view.
poiAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
poiAnnotationView.rightCalloutAccessoryView.tag = 1;
//call online navi by left accessory.
poiAnnotationView.leftCalloutAccessoryView.tag = 2;
return poiAnnotationView;
}
return nil;
}
技巧:
//该方法用于地图区域改变完成后会调用此接口(可以结合最后一个方法convertPoint:获得实时中心点的坐标) -->Uber
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"%s",__func__);
}
/*!
@brief 将相对于view的坐标转化为经纬度坐标
@param point 要转化的坐标
@param view point所基于的view
return 转化后的经纬度坐标
*/
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view;
如果你的工程只需要用到本文所述的几个功能,只需要下载我的demo,把需要的库导入.在你需要定位的控制器中继承mapViewController,你便可以实时获得用户的经纬度.(注意:标注必须要在右map的情况下才能使用,不然会报错)
(若不需要显示地图,只要定位和反地理编码.请参照demo中的testViewController)
Github: https://github.com/ouzhenxuan/gaodeMap
对你有用,请点star!THX