加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 在RN中获取当前用户坐标的最佳方法是什么

发布时间:2020-12-15 05:04:34 所属栏目:百科 来源:网络整理
导读:我想创建一个用于获取用户坐标的函数,但我发现我不确定我的方法是否足够好. 我想知道其他人如何编写这个函数,以便在ios和 android上100%确定. 现在我有这样的事情: function getUserLocation(callback: func) { Permissions.check('location') .then(respo
我想创建一个用于获取用户坐标的函数,但我发现我不确定我的方法是否足够好.
我想知道其他人如何编写这个函数,以便在ios和 android上100%确定.
现在我有这样的事情:
function getUserLocation(callback: func) {
  Permissions.check('location')
  .then(response => {
    if (response == 'authorized') {
      navigator.geolocation.getCurrentPosition(
        (location) => {
          callback({
              latitude: location.coords.latitude,longitude: location.coords.longitude
            },response);
        },(error) => {
          callback(null);
        },Platform.OS === 'android' ? null : {enableHighAccuracy: true,timeout: 100000,maximumAge: 1000}
      );
    } else {
      callback(null,response);
    }
  })
}

因此,您可以看到我想使用此函数,因此调用者可以知道是否允许使用位置以及是否获取当前坐标.
我正在尝试的是学习如何:
1.使这样的功能等待.
2.其他人如何使这样的功能(对于两种操作系统)和100%相同的功能.

你的问题分为两部分

1. Make function like this to be awaitable.

这直接通过为您提供如何操作的逻辑来回答.等待是什么,但实际上是异步函数或返回承诺的东西.如果你想要异步方式,那么你需要先创建一个返回promise的getCurrentPosition版本.这可以像下面这样完成

function getCurrentLocation(options) {
  return new Promise((resolve,reject) => {
    navigator.geolocation.getCurrentPosition(resolve,({code,message}) =>
      reject(Object.assign(new Error(message),{name: "PositionError",code})),options);
    });
};

PS:上述代码的积分为https://stackoverflow.com/a/44439358/2830850

现在您可以像下面那样重写代码

async function getUserLocation() {
  let response = await Permissions.check('location')
  let options = Platform.OS === 'android' ? null : {enableHighAccuracy: true,maximumAge: 1000}

  if (response == 'authorized') {
      return await getCurrentLocation(options)
  }

  return null;
}

如果你不想采取承诺方式,它就像下面

function getUserLocation() {
    return new Promise((resolve,reject) => {
        Permissions.check('location')
            .then(response => {
                if (response == 'authorized') {
                    navigator.geolocation.getCurrentPosition(
                        (location) => {
                            resolve({
                                latitude: location.coords.latitude,longitude: location.coords.longitude
                            });
                        },(error) => {
                            reject(error);
                        },Platform.OS === 'android' ? null : { enableHighAccuracy: true,maximumAge: 1000 }
                    );
                } else {
                    resolve(null);
                }
            })
    });
}

这里的一个关键是不同的回调,promise解析单个值,这意味着你需要使用对象来返回多个参数.

2. How others make function like this (for both OS) and to work 100% same.

这是你提出问题的地方,我可能会采取一些不同的方法,你可能会做其他事情而另一个人可能完全做其他事情.但我仍然会讨论一些可能性.

分成多个功能

function getUserLocation() {
   if (Platform.OS === 'android')
   {
       return getUserLocationAndroid();
   } else {
       return getUserLocationIOS();
   }
}

维护键值对

您可以拥有基于操作系统自动绑定功能的代码.

Utils = {
   getFunction(name) {
      if ((name + Platform.OS) in this)
          return this[name + Platform.OS]
      return this[name];
   }
   getLocation: function() {}
   //getLocationandroid: function() {}
}

因此,默认情况下,您将使用getLocation来同时为两个操作系统服务器,但是如果操作系统代码中的一个偏差太大,您将为其创建一个新功能.代码看起来如下所示

Utils.getFunction("getLocation")(...)

一个具有分支功能

这就是您目前的方法.只要功能易于理解,并且有明确的意图,代码的哪一部分可以处理平台特定的东西,我会坚持这一点

再次,可以在这里放置10种不同的方式,但它最终会取决于一件事,更容易维护,理解使用.一些方法也是特定于用例的,您可以根据用例混合多种方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读