国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

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

2020-07-26 02:29:14
字體:
來源:轉載
供稿:網友

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

- 定位到當前位置

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

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

- 選擇當前位置或者選擇目標位置進行發(fā)送

一.準備工作

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

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

3.根據項目的bundleID前往高德地圖API中創(chuàng)建引用并申請相關的key

高德地圖應用管理后臺

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

二.代碼部分

1.初始化高德地圖SDK

在AppleDelegate中引入相關頭文件,從高德地圖應用管理中找到該應用對用的key值,進行高德地圖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.定位到用戶當前位置

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

初始化地圖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];  //單次定位超時時間  [self.locationManager setLocationTimeout:6];  [self.locationManager setReGeocodeTimeout:3];}

開啟定位操作:

- (void)locateAction {  [self showHudInView:self.view hint:@"正在定位..."];  //帶逆地理的單次定位  [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {    if (error) {      [self showHint:@"定位錯誤" 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];    }  }];}

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

- (void)showMapPoint{  [_mapView setZoomLevel:15.1 animated: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:[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;    //設置氣泡可以彈出,默認為NO    annotationView.animatesDrop = YES;    //設置標注動畫顯示,默認為NO    annotationView.draggable = YES;    //設置標注可以拖動,默認為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];  //主動選擇地圖上的地點  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;}

包括主動選擇地圖上的點然后進行附近的搜索,可自定義搜索的內容,定位成功之后用戶可以獲取到當前的經緯度地址等一系列信息。詳細的代碼有點多就不一一貼出來了,需要的同學可以點擊下面的github地址去下載體驗。

github地址:https://github.com/XuZzzzzzzz/XCLocation

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 田东县| 通河县| 万宁市| 三门峡市| 澄江县| 和静县| 石楼县| 常熟市| 象山县| 荥阳市| 桦南县| 敦煌市| 色达县| 梁山县| 曲麻莱县| 河南省| 绵阳市| 庐江县| 海伦市| 长汀县| 安义县| 鄂伦春自治旗| 陈巴尔虎旗| 太谷县| 宁河县| 曲靖市| 灵武市| 惠东县| 聂拉木县| 合江县| 册亨县| 高唐县| 龙泉市| 民丰县| 高阳县| 东山县| 吉首市| 罗山县| 柳林县| 长宁县| 方正县|