1,再 build phases 中的 link binary with libraries 中 导入 CoreLocation.framework 库
2,包含:#import <CoreLocation/CoreLocation.h>
3,特别需要注意的: iPhone 机 需要链接网络, 否则获取的 placemarks is null
4,在info.plist 中添加
<key>NSLocationWhenInUseUsageDescription</key>
<string>此App需要您的同意才能访问您的位置</string>
@interface LocationTest : NSObject<CLLocationManagerDelegate>{
CLLocationDegrees lati;
CLLocationDegrees longti;
}
@property(nonatomic,strong)CLLocationManager *locationManager;
-(void)InitLocation;
//用户位置获取
-(void)InitLocation{
//初始化对象
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 1.0;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
// [self.locationManager requestAlwaysAuthorization]; // 永久授权
[self.locationManager requestWhenInUseAuthorization]; //使用中授权
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%@", error);
}
// 代理方法 地理位置反编码
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *newlocation = locations[0];
CLLocationCoordinate2D oCoordinate = newlocation.coordinate;
NSLog(@"经度:%f,维度:%f",oCoordinate.longitude,oCoordinate.latitude);
// 给经纬度全局属性赋值
lati = oCoordinate.latitude;
longti = oCoordinate.longitude;
[self.locationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newlocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
NSLog(@"----------reverseGeocodeLocation---------");
NSString* placeLity = @"";
NSString* str = @"";
for (CLPlacemark *place in placemarks) {
// CLPlacemark* place = placemarks[0];
NSDictionary *location =[place addressDictionary];
NSLog(@"国家:%@",[location objectForKey:@"Country"]);
NSLog(@"城市:%@",[location objectForKey:@"State"]);
NSLog(@"区:%@",[location objectForKey:@"SubLocality"]);
NSLog(@"位置:%@", place.name);
NSLog(@"国家:%@", place.country);
NSLog(@"城市:%@", place.locality);
placeLity = place.locality;
NSLog(@"区:%@", place.subLocality);
NSLog(@"街道:%@", place.thoroughfare);
NSLog(@"子街道:%@", place.subThoroughfare);
//字符串拼接
str = [NSString stringWithFormat:@"%@%@%@", place.locality, place.subLocality,place.thoroughfare];
}
NSLog(@"----- 位置 %@",str);
[LocationTest CallJsLocation:str andLati:lati andLongti:longti];
}];
NSLog(@"=====:%f,%f",lati,longti);
}
+(void)CallJsLocation:(NSString*)Adree andLati:(float)lati andLongti:(float)longti{
NSLog(@"%@ %f %f",Adree,lati,longti);
NSString *func = [NSString stringWithFormat:@"require('jsbScript').responseLocation('%@',%f,%f)",Adree,lati,longti];
// 转为C风格字符串
const char *stringFunc = [func UTF8String];
// OC调用JS
ScriptingCore::getInstance()->evalString(stringFunc);
}
//开启位置
+(void)StartLocation{
NSLog(@"---StartLocation----");
//初始化对象
LocationTest* locat = [[LocationTest alloc]init];
[locat InitLocation];
}