iOS仿微信發(fā)送實(shí)時(shí)位置(高德地圖)


最近項(xiàng)目中要寫(xiě)一個(gè)微信那種發(fā)送位置的功能。具體功能在于:

 - 定位到當(dāng)前位置
 - 可定位當(dāng)前位置附近的Poi
 - 可自行搜索目標(biāo)位置,并展示附近的poi
 - 選擇當(dāng)前位置或者選擇目標(biāo)位置進(jìn)行發(fā)送
IMG_8283.GIF

一.準(zhǔn)備工作

1.首先去高德地圖官網(wǎng)下載相關(guān)的SDK(如下圖):
image.png
2.按照高德地圖官網(wǎng)的步驟繼續(xù)添加所需要的依賴庫(kù)
image.png
3.根據(jù)項(xiàng)目的bundleID前往高德地圖API中創(chuàng)建引用并申請(qǐng)相關(guān)的key

高德地圖應(yīng)用管理后臺(tái)

到這一步前期的準(zhǔn)備工作基本差不多了,當(dāng)然,我這寫(xiě)的不是很具體,詳細(xì)的引入高德地圖SDK還需要
耐心按照高德地圖官網(wǎng)SDK一步步的去操作

二.代碼部分

1.初始化高德地圖SDK

在AppleDelegate中引入相關(guān)頭文件,從高德地圖應(yīng)用管理中找到該應(yīng)用對(duì)用的key值,進(jìn)行高德地圖SDK的初始化操作。直接貼代碼部分:

#import "AppDelegate.h"
#import "ViewController.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>

static NSString *APIKey = @"a1500980e29b7ca7612a46c19e0d2e3a";
@interface AppDelegate ()

@end
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
    [AMapServices sharedServices].apiKey = APIKey;
    
    return YES;
}

2.定位到用戶當(dāng)前位置

在你需要定位的類中引入與地圖相關(guān)的頭文件

初始化地圖view:

- (void)initMapView{
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64 + 44, SCREEN_WIDTH, 300)];
    self.mapView.delegate = self;
    self.mapView.mapType = MAMapTypeStandard;
    self.mapView.showsScale = NO;
    self.mapView.showsCompass = NO;
    self.mapView.showsUserLocation = YES;
    [self.view addSubview:self.mapView];
    
    UIButton *localButton = [UIButton buttonWithType:UIButtonTypeCustom];
    localButton.backgroundColor = [UIColor redColor];
    localButton.frame = CGRectMake(SCREEN_WIDTH - 60, 240, 50, 50);
    [localButton addTarget:self action:@selector(localButtonAction) forControlEvents:UIControlEventTouchUpInside];
    localButton.layer.cornerRadius = 25;
    localButton.clipsToBounds = YES;
    [localButton setImage:[UIImage imageNamed:@"定位"] forState:UIControlStateNormal];
    [self.mapView addSubview:localButton];
    
}
// 定位SDK

- (void)configLocationManager {
    self.locationManager = [[AMapLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //單次定位超時(shí)時(shí)間
    [self.locationManager setLocationTimeout:6];
    [self.locationManager setReGeocodeTimeout:3];
}

開(kāi)啟定位操作:

- (void)locateAction {
    [self showHudInView:self.view hint:@"正在定位..."];
    //帶逆地理的單次定位
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error) {
            [self showHint:@"定位錯(cuò)誤" yOffset:-180];
            NSLog(@"locError:{%ld - %@};",(long)error.code,error.localizedDescription);
            if (error.code == AMapLocationErrorLocateFailed) {
                return ;
            }
        }
        //定位信息
        NSLog(@"location:%@", location);
        if (regeocode)
        {
            [self hideHud];
            self.currentLocationCoordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
            self.city = regeocode.city;
            [self showMapPoint];
            [self setCenterPoint];
            self.request.location = [AMapGeoPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
            [self.mapSearch AMapPOIAroundSearch:self.request];
        }
    }];
}

定位成功之后展示大頭針到當(dāng)前位置(如果一直不顯示大頭針,檢查一下自己是否導(dǎo)入了高德地圖的資源文件):

- (void)showMapPoint{
    [_mapView setZoomLevel:15.1 animated:YES];
    [_mapView setCenterCoordinate:self.currentLocationCoordinate animated:YES];
}

- (void)setCenterPoint{
    MAPointAnnotation * centerAnnotation = [[MAPointAnnotation alloc] init];//初始化注解對(duì)象
    centerAnnotation.coordinate = self.currentLocationCoordinate;//定位經(jīng)緯度
    centerAnnotation.title = @"";
    centerAnnotation.subtitle = @"";
    [self.mapView addAnnotation:centerAnnotation];//添加注解
    
}
#pragma mark - MAMapView Delegate
- (MAAnnotationView *)mapView:(MAMapView *)mapView
            viewForAnnotation:(id<MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= YES;       //設(shè)置氣泡可以彈出,默認(rèn)為NO
        annotationView.animatesDrop = YES;        //設(shè)置標(biāo)注動(dòng)畫(huà)顯示,默認(rèn)為NO
        annotationView.draggable = YES;        //設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO
        annotationView.pinColor = MAPinAnnotationColorRed;
        return annotationView;
    }
    return nil;
}

地圖的代理方法等:

- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    [self.mapView removeAnnotations:self.mapView.annotations];
    
    CLLocationCoordinate2D centerCoordinate = mapView.region.center;
    self.currentLocationCoordinate = centerCoordinate;
    
    MAPointAnnotation * centerAnnotation = [[MAPointAnnotation alloc] init];
    centerAnnotation.coordinate = centerCoordinate;
    centerAnnotation.title = @"";
    centerAnnotation.subtitle = @"";
    [self.mapView addAnnotation:centerAnnotation];
    //主動(dòng)選擇地圖上的地點(diǎn)
    if (!self.isSelectedAddress) {
        [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
        self.selectedIndexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        self.request.location = [AMapGeoPoint locationWithLatitude:centerCoordinate.latitude longitude:centerCoordinate.longitude];
        self.currentPage = 1;
        self.request.page = self.currentPage;
        [self.mapSearch AMapPOIAroundSearch:self.request];
    }
    self.isSelectedAddress = NO;

}
包括主動(dòng)選擇地圖上的點(diǎn)然后進(jìn)行附近的搜索,可自定義搜索的內(nèi)容,定位成功之后用戶可以獲取到
當(dāng)前的經(jīng)緯度地址等一系列信息。詳細(xì)的代碼有點(diǎn)多就不一一貼出來(lái)了,需要的同學(xué)可以點(diǎn)擊下面的
github地址去下載體驗(yàn)。

github地址,記得給我小星星哦??

最后編輯于
?著作權(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)容

  • 最近在做基于高德地圖的定位、導(dǎo)航及添加大頭針的功能,特此記錄下來(lái)。。。方便剛接觸的同學(xué)參考。。。 一、申請(qǐng) Key...
    attackGiant閱讀 13,365評(píng)論 50 40
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • iOS:高德地圖的使用 本人花了點(diǎn)時(shí)間集成了高德地圖的幾乎所有的功能,包含:地圖的顯示、地圖的繪制、地圖的定位、地...
    小新xin閱讀 9,086評(píng)論 3 13
  • 跑步運(yùn)動(dòng)軟件最基本的功能之一,就是對(duì)運(yùn)動(dòng)中的用戶進(jìn)行實(shí)時(shí)定位,并繪制出運(yùn)動(dòng)路徑。本文主要內(nèi)容,就是用高德的SDK實(shí)...
    msq3閱讀 8,186評(píng)論 10 24
  • 關(guān)于馬克龍,關(guān)于那24歲,霧滿攔江的觀點(diǎn),覺(jué)得人家那是真愛(ài),愛(ài)的高階是成就自己。 正所謂英雄所見(jiàn)略同!馬克龍的新聞...
    云居雁閱讀 745評(píng)論 0 1

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