React-native Android geolocation

自古美人都是妖i 提交于 2019-12-06 04:16:20

问题


I need to detect user's geolocation on React-native Android but the current implementation supports only iOS. I haven't found any tutorials or related questions anywhere.

I have tried to create my own Android native component for this. I have tried it with the emulator (setting some coordinates via commandline) and with a phone but there is no gps icon in the action bar and the react callback has no data in it.

I'm trying to get the last known location immediately and I'm also setting up an event listener for updates.

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){

  }
}

Permissions:

<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" />

I haven't found anything relevant from the logcat logs either. Any tips what is wrong with this code or any kind of solution for getting geocoordinates would be great!


回答1:


UPDATE: Sigh. I spoke to soon. It's part of 0.15.0: https://github.com/facebook/react-native/releases/tag/v0.15.0

The official module appears not yet open-sourced (See: Known Issues), but the fact that there's documentation for using it with Android suggests it'll show up sometime… soon? In the mean time, here's what I have a month later and after a bit of googling:

  • rn-geolocation (Looks straightforward. Going to give this a try.)
  • react-native-android-geolocation (Also looks good?)
  • react-native-background-geolocation (Non-free for Android but looks like the real deal for serious GPS-intensive apps like running/navigation stuff)
  • react-native-geolocation-android (LGPL.)


来源:https://stackoverflow.com/questions/33341760/react-native-android-geolocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!