美文网首页
2019-12-09 RN 高德地图使用 react-nativ

2019-12-09 RN 高德地图使用 react-nativ

作者: 指尖_跳舞 | 来源:发表于2019-12-09 14:36 被阅读0次

RN 使用高德地图 时候通过多方比较选择react-native-amap-geolocation ,由于插件是0.6版本之前的所以要通过pod安装的话,需要修改Podfile文件:

pod 'react-native-amap-geolocation', :path => '../node_modules/react-native-amap-geolocation/lib/ios'

另外,使用一次定位方法时候,Geolocation.getCurrentPosition()时候,设置

setLocatingWithReGeocode(true); // ios 还是没有逆地址定位 即reGeocode为nil,通过多次调试发现是 通过

[_manager startUpdatingLocation] 方法调用的定位 (多次定位)有很大的概率在第一次定位的时候会发生逆地址信息reGeocode为nil的情况,推荐使用requestLocationWithReGeocode方法即在AMapGeolocation.m文件中添加如下方法:
RCT_EXPORT_METHOD(requestLocationWithReGeocode:(BOOL) withReGeocode) {

    [_manager requestLocationWithReGeocode:withReGeocode completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {

        if(error){

            [self sendEventWithName:@"AMapGeolocation"

            body:@{

              @"errorCode":@(error.code),

              @"errorInfo": error.localizedDescription,

            }];

        }else{

            idjson = [selfjson:locationreGeocode:regeocode];

            [selfsendEventWithName:@"AMapGeolocation"body:json];

        }

    }];

}

然后在js文件中将 Geolocation.getCurrentPosition 方法中的 _1.start();改为_1.requestLocationWithReGeocode(true);(需要在index文件中导出)即可。 当然最好的方法还是自己封装,这样修改第三方库会有很多麻烦,这里只是提供思路,有时间的话还是自己封装一下吧。

相关文章

网友评论

      本文标题:2019-12-09 RN 高德地图使用 react-nativ

      本文链接:https://www.haomeiwen.com/subject/zmbbgctx.html