美文网首页eiYoiOS开发iOS技术专题
iOS 高德地图(继承即可使用定位\反编码)

iOS 高德地图(继承即可使用定位\反编码)

作者: Figo_OU | 来源:发表于2015-07-30 09:42 被阅读16926次
效果图

高德地图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

相关文章

网友评论

  • PGOne爱吃饺子:楼主,可否问你一个问题
  • 开飞机的叔客:楼主切换到后台或者锁屏还能够定位吗
    Figo_OU:可以的
  • 翻滚的炒勺2013:demo 不能用
  • GodLoveNan:onReGeocodeSearchDone 不回调
  • 少年_如他:楼主,请问一下,系统的气泡,设置他一进入地图页面就弹出来,就是设置poiAnnotationView.selected = YES,但是这样设置了以后,气泡还是弹不出来,请问怎么回事呢
  • Icanbe:您好,您遇到过用完离线缓存地图之后 流量用的也很多的情况么
  • 72122e3c4c93:我用cocopods导入高德SDK,为什么不能调用AMapSeacher
    神经病姐姐的日常:@古彰彰 我刚才解决了,把版本降一下,
    pod 'AMap3DMap', '2.5.0'
    pod 'AMapSearch', '3.3.0' #搜索服务SDK
  • e977f48d7c94:不是利用系统的截图,而是用代码截图
  • e977f48d7c94:请问楼主,你知道截图的时候为神马高德地图会是空白一片的呢?
  • 叶舞清风:谢谢分享
  • 二挠:libc++abi.dylib: terminating with uncaught exception of type NSException。。。。。。求解
    d4d9eef0b549:@二挠 第三方依赖库有问题,重新添加
  • Wow_我了个去:[self.search AMapReGoecodeSearch:regeo]; 这句总是崩溃什么原因,你说的继承?
    a0932ff1578d:@Wow_我了个去 我显示的崩溃原因是unrecognized selector sent to instance 0x1477d71c0
    Wow_我了个去:@区振轩 已找到问题。谢谢 :smiley:
    Figo_OU:@Wow_我了个去 崩溃的log总该贴上吧。(很大原因是回调方法没有实现)

本文标题:iOS 高德地图(继承即可使用定位\反编码)

本文链接:https://www.haomeiwen.com/subject/hvgmqttx.html