iOS 高德地圖(繼承即可使用定位\反編碼)

效果圖

高德地圖API文檔:

這里先說(shuō)定位(在地圖中顯示用戶位置,獲取經(jīng)緯度),逆地理編碼
值得注意的是,高德地圖的mapview對(duì)象不能多次創(chuàng)建,最好是使用單實(shí)例.不然程序很容易會(huì)崩

第一步:配置環(huán)境
1>.到http://lbs.amap.com/api/ios-sdk/down/ 上下載SDK(本文都是使用2D的SDK,3D的請(qǐng)自己查看.)
2>.注冊(cè)賬號(hào),獲得appkey http://id.amap.com/?type=spa&ref=http://lbs.amap.com/
3>.添加系統(tǒng)庫(kù) http://lbs.amap.com/api/ios-sdk/guide/project/
4>.在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。

第二步:開(kāi)始編程了
1>.首先要在appDeletage中寫(xiě)上key :
[MAMapServices sharedServices].apiKey = @"5aed7f0e8121d1d985e3344f98ca5955";
2>.在viewDidLoad中實(shí)例化一個(gè)mapview對(duì)象(這點(diǎn)很重要,在官方demo中將這一句放在外面了很難找出來(lái))

self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
self.mapView.frame = self.view.bounds;
self.mapView.delegate = self;//設(shè)置代理也重要
[self.view addSubview:self.mapView];

實(shí)例化地圖實(shí)例以后:

self.mapView.showsUserLocation = YES;//這句就是開(kāi)啟定位
    self.mapView.userTrackingMode = MAUserTrackingModeFollow; // 追蹤用戶位置.

開(kāi)始定位以后,MAUserLocation * userLocat = self.mapView.userLocation;
就可以通過(guò)地圖實(shí)例獲得位置了.如:
CGFloat lat = self.mapView.userLocation.coordinate.latitude;

要實(shí)時(shí)獲得用戶的經(jīng)緯度:則需要添加下面這個(gè)代理方法

-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if (updatingLocation) {
        NSLog(@"latitude : %f , longitude : %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}

3>初始化完地圖實(shí)例,還要弄個(gè)搜索實(shí)例用于反編碼:最好也是在viewDidLoad中搞啦

self.search = [[AMapSearchAPI alloc] initWithSearchKey:[MAMapServices sharedServices].apiKey Delegate:self];

官方demo中delegate寫(xiě)了個(gè)nil上去,直接copy了.組長(zhǎng)突然過(guò)來(lái)看進(jìn)度;我擦,害我被組長(zhǎng)叼了一頓;

4>開(kāi)始反編碼

MAUserLocation * userLocat = self.mapView.userLocation;
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.location = [AMapGeoPoint locationWithLatitude:userLocat.coordinate.latitude longitude:userLocat.coordinate.longitude];
    regeo.requireExtension = YES;
     //發(fā)起逆地理編碼
    [self.search AMapReGoecodeSearch:regeo];

弄完發(fā)請(qǐng)求之后,還要搞個(gè)delegate的回調(diào).

/* 逆地理編碼回調(diào). */
- (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];//要添加標(biāo)注
        [self.mapView selectAnnotation:reGeocodeAnnotation animated:YES];//標(biāo)注是否有動(dòng)畫(huà)效果
    }
}

如果要添加標(biāo)注:那么就要實(shí)現(xiàn)下面這個(gè)方法

- (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;
}

技巧:
//該方法用于地圖區(qū)域改變完成后會(huì)調(diào)用此接口(可以結(jié)合最后一個(gè)方法convertPoint:獲得實(shí)時(shí)中心點(diǎn)的坐標(biāo)) -->Uber

- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    NSLog(@"%s",__func__);
}

/*!
 @brief 將相對(duì)于view的坐標(biāo)轉(zhuǎn)化為經(jīng)緯度坐標(biāo)
 @param point 要轉(zhuǎn)化的坐標(biāo)
 @param view point所基于的view
 return 轉(zhuǎn)化后的經(jīng)緯度坐標(biāo)
 */
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view;

如果你的工程只需要用到本文所述的幾個(gè)功能,只需要下載我的demo,把需要的庫(kù)導(dǎo)入.在你需要定位的控制器中繼承mapViewController,你便可以實(shí)時(shí)獲得用戶的經(jīng)緯度.(注意:標(biāo)注必須要在右map的情況下才能使用,不然會(huì)報(bào)錯(cuò))
(若不需要顯示地圖,只要定位和反地理編碼.請(qǐng)參照demo中的testViewController)

Github: https://github.com/ouzhenxuan/gaodeMap
對(duì)你有用,請(qǐng)點(diǎn)star!THX

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容