天天看點

iOS仿微信發送實時位置(高德地圖

最近項目中要寫一個微信那種發送位置的功能。具體功能在于:

- 定位到目前位置

- 可定位目前位置附近的Poi

- 可自行搜尋目标位置,并展示附近的poi

- 選擇目前位置或者選擇目标位置進行發送

在這裡我還是要推薦下我自己建的iOS開發學習群:680565220,群裡都是學ios開發的,如果你正在學習ios ,小編歡迎你加入,今天分享的這個案例已經上傳到群檔案,大家都是軟體開發黨,不定期分享幹貨(隻有iOS軟體開發相關的),包括我自己整理的一份2018最新的iOS進階資料和進階開發教程

iOS仿微信發送實時位置(高德地圖

一.準備工作

1.首先去高德地圖官網下載下傳相關的SDK(如下圖):

need-to-insert-img

iOS仿微信發送實時位置(高德地圖

2.按照高德地圖官網的步驟繼續添加所需要的依賴庫

need-to-insert-img

iOS仿微信發送實時位置(高德地圖

3.根據項目的bundleID前往高德地圖API中建立引用并申請相關的key

高德地圖應用管理背景

到這一步前期的準備工作基本差不多了,當然,我這寫的不是很具體,詳細的引入高德地圖SDK還需要

耐心按照高德地圖官網SDK一步步的去操作

二.代碼部分

1.初始化高德地圖SDK

在AppleDelegate中引入相關頭檔案,從高德地圖應用管理中找到該應用對用的key值,進行高德地圖SDK的初始化操作。直接貼代碼部分:

#import"AppDelegate.h"

#import"ViewController.h"

#import <AMapLocationKit/AMapLocationKit.h>

#import <AMapFoundationKit/AMapFoundationKit.h>

staticNSString*APIKey =@"a1500980e29b7ca7612a46c19e0d2e3a";

@interfaceAppDelegate()

@end

@implementationAppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

[self.window makeKeyAndVisible];

self.window.rootViewController = [[UINavigationControlleralloc] initWithRootViewController:[ViewController new]];

[AMapServices sharedServices].apiKey = APIKey;

returnYES;

}

2.定位到使用者目前位置

在你需要定位的類中引入與地圖相關的頭檔案

初始化地圖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 = [UIButtonbuttonWithType:UIButtonTypeCustom];

localButton.backgroundColor = [UIColorredColor];

localButton.frame =CGRectMake(SCREEN_WIDTH -60,240,50,50);

[localButton addTarget:selfaction:@selector(localButtonAction) forControlEvents:UIControlEventTouchUpInside];

localButton.layer.cornerRadius =25;

localButton.clipsToBounds =YES;

[localButton setImage:[UIImageimageNamed:@"定位"] forState:UIControlStateNormal];

[self.mapView addSubview:localButton];

}

// 定位SDK

- (void)configLocationManager {

self.locationManager = [[AMapLocationManager alloc] init];

[self.locationManager setDelegate:self];

[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];

//單次定位逾時時間

[self.locationManager setLocationTimeout:6];

[self.locationManager setReGeocodeTimeout:3];

}

開啟定位操作:

- (void)locateAction {

[selfshowHudInView:self.view hint:@"正在定位..."];

//帶逆地理的單次定位

[self.locationManager requestLocationWithReGeocode:YEScompletionBlock:^(CLLocation*location, AMapLocationReGeocode *regeocode,NSError*error) {

if(error) {

[selfshowHint:@"定位錯誤"yOffset:-180];

NSLog(@"locError:{%ld - %@};",(long)error.code,error.localizedDescription);

if(error.code == AMapLocationErrorLocateFailed) {

return;

}

}

//定位資訊

NSLog(@"location:%@", location);

if(regeocode)

{

[selfhideHud];

self.currentLocationCoordinate =CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);

self.city = regeocode.city;

[selfshowMapPoint];

[selfsetCenterPoint];

self.request.location = [AMapGeoPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];

[self.mapSearch AMapPOIAroundSearch:self.request];

}

}];

}

定位成功之後展示大頭針到目前位置(如果一直不顯示大頭針,檢查一下自己是否導入了高德地圖的資源檔案):

- (void)showMapPoint{

[_mapView setZoomLevel:15.1animated:YES];

[_mapView setCenterCoordinate:self.currentLocationCoordinate animated:YES];

}

- (void)setCenterPoint{

MAPointAnnotation * centerAnnotation = [[MAPointAnnotation alloc] init];//初始化注解對象

centerAnnotation.coordinate =self.currentLocationCoordinate;//定位經緯度

centerAnnotation.title =@"";

centerAnnotation.subtitle =@"";

[self.mapView addAnnotation:centerAnnotation];//添加注解

}

#pragma mark - MAMapView Delegate

- (MAAnnotationView *)mapView:(MAMapView *)mapView

viewForAnnotation:(id<MAAnnotation>)annotation {

if([annotation isKindOfClass:[MAPointAnnotationclass]]) {

staticNSString*pointReuseIndentifier =@"pointReuseIndentifier";

MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

if(annotationView ==nil)

{

annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];

}

annotationView.canShowCallout=YES;//設定氣泡可以彈出,預設為NO

annotationView.animatesDrop =YES;//設定标注動畫顯示,預設為NO

annotationView.draggable =YES;//設定标注可以拖動,預設為NO

annotationView.pinColor = MAPinAnnotationColorRed;

returnannotationView;

}

returnnil;

}

地圖的代理方法等:

- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

[self.mapView removeAnnotations:self.mapView.annotations];

CLLocationCoordinate2DcenterCoordinate = mapView.region.center;

self.currentLocationCoordinate = centerCoordinate;

MAPointAnnotation * centerAnnotation = [[MAPointAnnotation alloc] init];

centerAnnotation.coordinate = centerCoordinate;

centerAnnotation.title =@"";

centerAnnotation.subtitle =@"";

[self.mapView addAnnotation:centerAnnotation];

//主動選擇地圖上的地點

if(!self.isSelectedAddress) {

[self.tableView setContentOffset:CGPointMake(0,0) animated:NO];

self.selectedIndexPath=[NSIndexPathindexPathForRow:0inSection: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;

}

包括主動選擇地圖上的點然後進行附近的搜尋,可自定義搜尋的内容,定位成功之後使用者可以擷取到