当前位置 : 主页 > 网页制作 > React >

React-native Android地理定位

来源:互联网 收集:自由互联 发布时间:2021-06-15
我需要在React-native Android上检测用户的地理位置,但当前的实现仅支持iOS.我没有在任何地方找到任何教程或相关问题. 我试图为此创建自己的Android原生组件.我已经尝试使用模拟器(通过命
我需要在React-native Android上检测用户的地理位置,但当前的实现仅支持iOS.我没有在任何地方找到任何教程或相关问题.

我试图为此创建自己的Android原生组件.我已经尝试使用模拟器(通过命令行设置一些坐标)和手机,但操作栏中没有gps图标,并且反应回调中没有数据.

我正在尝试立即获取最后一个已知位置,我也正在设置事件监听器以进行更新.

public class GeoLocation extends ReactContextBaseJavaModule implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

protected static final String TAG = "GeoLocation";
protected GoogleApiClient mGoogleApiClient;
protected Location mLastLocation;


public GeoLocation(ReactApplicationContext reactContext) {
    super(reactContext);
    buildGoogleApiClient();
}

@Override
public String getName() {
    return "GeoLocation";
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(getReactApplicationContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle connectionHint) {

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    //WritableMap params = Arguments.createMap();

    //Can I use ConcurrentHashMap instead of WritableMap?
    ConcurrentHashMap<String, String> params = new ConcurrentHashMap();
    params.put("lastLocation", mLastLocation.toString());
    sendEvent(getReactApplicationContext(), "geoCoordsChanged", params);
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}

@Override
public void onConnectionSuspended(int cause) {
    mGoogleApiClient.connect();
}

@ReactMethod
public void getLastLocation(Callback success,Callback err){
    try {
        String msg = "No location found yet";

        if(mLastLocation != null){
            msg = mLastLocation.toString();
        }

        success.invoke(msg);
    } catch (Exception e) {
        err.invoke(e.getMessage());
    }
}

private void sendEvent(ReactContext reactContext,
                       String eventName,
                       @Nullable ConcurrentHashMap<String, String> params) {
  try {
      reactContext
          .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
          .emit(eventName, params);
  } catch(Exception e){

  }
}

权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:theme="@style/AppTheme">

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />

我还没有从logcat日志中找到任何相关内容.任何提示这个代码有什么问题或任何一种获得地理坐标的解决方案都会很棒!

更新:叹息.我很快就说了.这是0.15.0: https://github.com/facebook/react-native/releases/tag/v0.15.0的一部分

官方模块似乎尚未开源(参见:Known Issues),但事实上有documentation for using it with Android表明它会在某个时间出现……很快?与此同时,这是我一个月后和谷歌谷歌搜索后的情况:

> rn-geolocation(看起来很简单.试试这个.)
> react-native-android-geolocation(看起来还不错?)
> react-native-background-geolocation(Android版非免费,但对于严重的GPS密集型应用程序,如运行/导航内容,它看起来真的很划算)
> react-native-geolocation-android(LGPL.)

网友评论