首页 > 解决方案 > react-native 如何定位移动应用的用户?

问题描述

我想在 react-native 中添加位置。我在 react-native 中有一个简单的应用程序。我想添加使用该应用程序的用户的地理位置。想法是在 Laravel 的管理面板上显示移动用户的当前位置。移动应用程序将在 laravel 中的 react-native 和 web 上。我正在寻找指导如何做到这一点。

标签: javascriptreact-nativegeolocation

解决方案


您可以为此使用react-native-geolocation-service库。

使用此 cmd 安装库npm install react-native-geolocation-service

并像这样实现它:

...
import Geolocation from 'react-native-geolocation-service';
...

componentDidMount() {
  if (hasLocationPermission) {
    Geolocation.getCurrentPosition(
        (position) => {
          //here you'll get your current location
          console.log(position);
        },
        (error) => {
          // See error code charts below.
          console.log(error.code, error.message);
        },
        { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );
  }
}

希望这对你有用。


推荐阅读