ios端可以直接获取经纬度
this.$geo.get().then(
data => {
this.$notice.alert({
message:
'经度: ' +
data.locationLng +
'\r\n\r\n纬度: ' +
data.locationLat,
okTitle: '确认',
callback () {
点击确认按钮的回调
}
});
},
error => {
this.$notice.toast({
message: '获取位置失败'
});
console.log(error);
}
);
此时没有获取具体的地理位置
逐步分析
1.查看封装的geo.js
const geolocation = weex.requireModule('bmGeolocation')
const Geolocation = Object.create(null)
Geolocation.install = (Vue) => {
Vue.prototype.$geo = {
get () {
return new Promise((resolve, reject) => {
geolocation.getGeolocation(({ status, errorMsg, data }) => {
status === 0 ? resolve(data) : reject({ status, errorMsg, data })
})
})
}
}
}
Vue.use(Geolocation)
weex.requireModule('bmGeolocation')
引入的是这个,全局搜索bmGeolocation
这里是注册,真正调用的是
BMGeolocationModule类
头文件中遵守了WXModuleProtocol协议
实现文件中
@synthesize weexInstance;
WX_EXPORT_METHOD(@selector(getGeolocation:))
- (void)getGeolocation:(WXModuleCallback)callback
{
[[JYTLocationManager shareInstance] getCurrentLocation:^(NSString *lon, NSString *lat,NSString *city,NSString *subLocality,NSString *street) {
if (callback) {
NSInteger resCode = BMResCodeError;
NSDictionary *data = nil;
if (lon && lat) {
resCode = BMResCodeSuccess;
data = @{@"locationLat": lat,@"locationLng": lon,@"city":city,@"subLocality":subLocality,@"street":street};
}
/* 构建callback数据 */
NSDictionary *resultData = [NSDictionary configCallbackDataWithResCode:resCode msg:nil data:data];
callback(resultData);
}
}];
}
原先只有经度和纬度NSString *lon, NSString *lat两个字段的,现在拓展多三个字段NSString *subLocality,NSString *street,城市,区域,和街道。
里面是调用JYTLocationManager类获取经纬度的
这个方法里面获取到位置,所以在这里增加反向地理编码,把经纬度转为具体地址。
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations firstObject]; //取出第一个位置
/* 将gps坐标系转换成gcj-02坐标系 */
CLLocationCoordinate2D coordinate = [TransformCLLocation wgs84ToGcj02:location.coordinate];
[self.locationManager stopUpdatingLocation];
//反向地理编码
CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
CLLocation *cl = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
__block NSString *city=@"";
__block NSString *subLocality = @"";
__block NSString *street = @"";
[clGeoCoder reverseGeocodeLocation:cl completionHandler: ^(NSArray *placemarks,NSError *error) {
for (CLPlacemark *placeMark in placemarks) {
NSDictionary *addressDic = placeMark.addressDictionary;
NSString *state=[addressDic objectForKey:@"State"];
city=[addressDic objectForKey:@"City"];
subLocality=[addressDic objectForKey:@"SubLocality"];
street=[addressDic objectForKey:@"Street"];
NSLog(@"所在城市====%@ %@ %@ %@", state, city, subLocality, street);
[self callBackWithLongitude:[NSString stringWithFormat:@"%f",coordinate.longitude] latitude:[NSString stringWithFormat:@"%f",coordinate.latitude] city:city subLocality:subLocality street:street];
[_locationManager stopUpdatingLocation];
}
}];
}
最后使用
this.$geo.get().then(
data => {
console.log('获取到城市city=',data.city)
},
error => {
this.$notice.toast({
message: '获取位置失败'
});
console.log(error);
}
);
最后发现是个坑,一旦pod update,自己修改的第三方代码,代码就会重置!!!
只能自己再封装一个原生代码制作成pod,供js调用。