你的位置:首页 > 软件开发 > 操作系统 > IOS实现自动定位和手动选择城市功能

IOS实现自动定位和手动选择城市功能

发布时间:2015-10-08 14:00:08
IOS自动定位使用的是高德地图SDK在高德开放平台http://lbs.amap.com/api/ios-sdk/down/ 下载2D地图SDK和搜索SDK将SDK导入工程内 按照高德的配置说明进行配置项目最后需要提醒 在高德的SDK中有些文件夹使用的是快捷方式, 如果你在你 ...

IOS自动定位使用的是高德地图SDK

在高德开放平台http://lbs.amap.com/api/ios-sdk/down/ 下载2D地图SDK和搜索SDK

将SDK导入工程内 按照高德的配置说明进行配置项目

最后需要提醒 在高德的SDK中有些文件夹使用的是快捷方式, 如果你在你本地的工程编译通过, 但可能在你的服务端自动打包的时候不能自动编译通过

需要将那些快捷方式的文件夹用真实文件夹替换掉就可以了。

在工程中导入

#import "MAMapKit/MAMapKit.h"

#import "AMapSearchKit/AMapCommonObj.h"

#import "AMapSearchKit/AMapSearchAPI.h"

在Controller中使用初始化地图服务

#pragma mark MAMAP init AND 定位回调- (void)initMap{   if (![CLLocationManager locationServicesEnabled]) {    [PXAlertView showAlertWithTitle:@"提示" message:[NSString stringWithFormat:@"请开启定位:设置 > 隐私 > 位置 > 定位服务"] completion:^(BOOL cancelled, NSInteger buttonIndex) {    }];    return;  }  else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){    [PXAlertView showAlertWithTitle:@"提示" message:[NSString stringWithFormat:@"定位失败,请开启定位:设置 > 隐私 > 位置 > 定位服务 下 应用"] completion:^(BOOL cancelled, NSInteger buttonIndex) {    }];    return;  }  [MAMapServices sharedServices].apiKey = MAMAP_APPKEY;//高德KEY  _mapView          = [[MAMapView alloc] init];  _mapView.delegate      = self;  _mapView.showsUserLocation = YES;  //[_mapView setUserTrackingMode:MAUserTrackingModeFollowWithHeading animated:YES];    point  = [[AMapGeoPoint alloc] init];  _search = [[AMapSearchAPI alloc] initWithSearchKey:MAMAP_APPKEY                       Delegate:self];  regeoRequest      = [[AMapReGeocodeSearchRequest alloc] init];  regeoRequest.searchType = AMapSearchType_ReGeocode;  regeoRequest.radius   = 50;  regeoRequest.requireExtension = YES;}// 定位回调- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{  if (updatingLocation) {        point.latitude = userLocation.coordinate.latitude;    point.longitude = userLocation.coordinate.longitude;        regeoRequest.location = [AMapGeoPoint locationWithLatitude:point.latitude                             longitude:point.longitude];    // 发起逆地理编码    [_search AMapReGoecodeSearch:regeoRequest];     _mapView.showsUserLocation =NO;      }}// 逆地理编码回调- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{  if (response.regeocode != nil) {        NSString *province=response.regeocode.addressComponent.province;    NSString *city=response.regeocode.addressComponent.city;    NSString *district=response.regeocode.addressComponent.district;    NSString *value=nil;    NSString *key=nil;        NSMutableArray *cityArrayTemp=nil;    for (AreaModel *model in provinceArray) {      if ([model.value isEqualToString:province]) {        cityArrayTemp=[NSMutableArray arrayWithArray:model.children];      }    }    NSMutableArray *districtArryTemp=nil;    if (cityArrayTemp) {      if (city.length>0) {        for (AreaModel *cityModel in cityArrayTemp) {          if ([cityModel.value isEqualToString:city]) {            districtArryTemp=[NSMutableArray arrayWithArray:cityModel.children];          }        }      }      else{        //直辖市        for (AreaModel *cityModel in cityArrayTemp) {          if ([cityModel.value isEqualToString:district]) {            value=cityModel.value;            key=cityModel.key;          }        }      }    }    if (districtArryTemp) {      for (AreaModel *provinceModel in districtArryTemp) {        if ([provinceModel.value isEqualToString:district]) {          value=provinceModel.value;          key=provinceModel.key;        }      }    }    self.keyCode=key;    [self.checkCityButton setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,district] forState:UIControlStateNormal];      }}

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:IOS实现自动定位和手动选择城市功能

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。