android - React-native Android 地理定位

标签 android geolocation react-native

我需要在 React-native Android 上检测用户的地理位置,但当前的实现仅支持 iOS。我没有在任何地方找到任何教程或相关问题。

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

我正在尝试立即获取最后一个已知位置,并且我还在为更新设置一个事件监听器。

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建议它会在某个时候出现……很快?与此同时,这是我一个月后在谷歌搜索后得到的:

关于android - React-native Android 地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33341760/

相关文章:

geolocation - 如何读取GDAL中像素的地理坐标?

android - 阵列状态未设置

android - 安装 gradle 以在 cordova build android 中使用

java - 从寻呼机对象访问播放列表列表

android - 收到同样的消息

android - 为什么 google place api 给我的结果为零?

Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER) 在现实生活中多久返回一次 null?

android - ListView 显示排序的数据而不是它在数据库中的存储方式

function - 打开 native 屏幕时调用函数

react-native - 无法展开和关闭可折叠点击