一
、
配置.framework形式开发包
第一步:适配iOS9.2,iOS8等
NSLocationWhenInUseUsageDescription BOOL 类型 数值为YES(使用期间)
NSLocationAlwaysUsageDescription BOOL 类型 数值为YES(始终使用)二者选择其一,如果两个都用默认使用NSLocationAlwaysUsageDescription
第二步:导入所需的系统库
CoreLocation.framework
QuartzCore.framework、
OpenGLES.framework、
SystemConfiguration.framework、
CoreGraphics.framework、
Security.framework、
CoreTelephony.framework 、
libsqlite3.0.tbd、
libstdc++.6.0.9.tbd
第三步:配置环境
1 在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。
2 静态库中采用Objective-C++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即将Xcode的Project -> Edit Active Target -> Build -> GCC4.2 - Language -> Compile Sources As设置为"Objective-C++”,但是实际应用中前者的方法可用,后者的方法会引起其他语言的不可用,所以使用前者的方法为佳。
二、
注册百度地图(授权百度地图)
第一步:在APPDelete.h文件里加入
#import "BMapKit.h"
BMKGeneralDelegate
BMKMapManager *mapManager;
第二步:在APPDelete.m文件里加入
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//注册百度地图
[self reguestBaiduMap];
return YES;
}
#pragma mark 注册百度地图
- (void)reguestBaiduMap
{
// 要使用百度地图,请先启动BaiduMapManager
mapManager = [[BMKMapManager alloc]init];
BOOL ret = [mapManager start:@"3Fo6RpUAQzxT2Cdtg6vKelpV" generalDelegate:self];
if (!ret) {
NSLog(@"启动失败!");
}else{
NSLog(@"启动成功!");
}
}
#pragma mark 百度地图注册回调
- (void)onGetNetworkState:(int)iError
{
if (0 == iError) {
NSLog(@"联网成功");
}
else{
NSLog(@"onGetNetworkState %d",iError);
}
}
- (void)onGetPermissionState:(int)iError
{
if (0 == iError) {
NSLog(@"授权成功");
}
else {
NSLog(@"onGetPermissionState %d",iError);
}
}
二
、
定位城市,获取经纬度,获取城市名
#import "BMapKit.h"
#import "BMKGeocodeSearch.h"
BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate
BMKLocationService *_locService;
//坐标
CLLocationCoordinate2D pt;
BMKGeoCodeSearch *_geocodesearch;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//注册百度地图
[self reguestBaiduMap];
_locService = [[BMKLocationService alloc]init];
_geocodesearch = [[BMKGeoCodeSearch alloc]init];
_locService.delegate = self;
_geocodesearch.delegate = self;
[_locService startUserLocationService];
}
//实现相关delegate 处理位置信息更新
//处理方向变更信息 BMKLocationServiceDelegate的代理
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
}
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
//停止定位
[_locService stopUserLocationService];
//把经度纬度保存到本地
NSString *latitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.longitude];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:latitude forKey:@"latitude"];
[defaults setObject:longitude forKey:@"longitude"];
[defaults synchronize];
//根据经度纬度检索经度纬度信息检索成功之后返回城市
[self getData:longitude with:latitude];
}
#pragma mark -----根据地理坐标返回信息
-(void)getData:(NSString *)coordinateXText with:(NSString *)coordinateYText
{
pt = (CLLocationCoordinate2D){[coordinateYText floatValue], [coordinateXText floatValue]};
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeocodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
if(flag)
{
NSLog(@"反geo检索发送成功");
}
else
{
NSLog(@"反geo检索发送失败");
}
}
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
//把城市信息保存到本地
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:result.addressDetail.city forKey:@"city"];
[defaults synchronize];
NSLog(@"%@",result.addressDetail.city);
}