Mabox Android - 没有获得位置更新

时间:2017-07-29 19:20:29

标签: android geolocation location mapbox mapbox-android

我正在使用Mapbox SDK,我想将地图移动并缩放到当前位置。如果我启用位置图层,我会在正确的位置获得点。但是我的位置引擎监听器没有被调用,所以我无法移动相机。

onCreateView中,我开始与GoogleApiClient建立联系。一旦连接,我用它来检查位置设置,看看它们是否已打开(在检查位置许可后)。如果成功,我这样做:

if (locationEngine == null) {
   locationEngine = new LocationSource(getActivity());
   locationEngine.requestLocationUpdates();
}
Location location = locationEngine.getLastLocation(); // This returns null

onLocationChanged(location); // This moves the camera if a location is passed in

locationEngine.addLocationEngineListener(locationEngineListener);

// This works, the location layer functions properly    
getMap(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {

                mapboxMap.setMyLocationEnabled(true);
            }
        });

以下是locationEngineListener的作用:

private class Listener implements LocationEngineListener {
        @Override
        public void onConnected() {
            // No action needed here.
        }

        @Override
        public void onLocationChanged(Location location) {

            CurrentLocationMap.this.onLocationChanged(location);
        }
    }

问题是永远不会调用onLocationChanged

1 个答案:

答案 0 :(得分:2)

这是完全有效的解决方案,可能在不同的情况下。但我想添加一些小解释步骤,以便任何人都能获得确切的概念。我没有在您的代码中看到您正在使用的LocationEngine。在这里,我使用LocationServices.FusedLocationApi作为我的LocationEngine。

1)Android组件的onCreate()(例如,活动片段服务注意:不是IntentService ),构建,然后连接 GoogleApiClient,如下所示。

buildGoogleApiClient();
mGoogleApiClient.connect();

其中,buildGoogleApiClient()实现是,

protected synchronized void buildGoogleApiClient() {
        Log.i(TAG, "Building GoogleApiClient");

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

    }

稍后在onDestroy()上,您可以将GoogleApiClient与

断开连接
@Override
    public void onDestroy() {
        Log.i(TAG, "Service destroyed!");
        mGoogleApiClient.disconnect();
        super.onDestroy();
    }

第1步确保您构建并连接GoogleApiClient。

1)GoogleApiClient实例第一次在方法onConnected()上连接。现在,您的下一步应该是onConnected()方法。

@Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.i(TAG, "GoogleApiClient connected!");
        buildLocationSettingsRequest();
        createLocationRequest();
        location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        Log.i(TAG, " Location: " + location); //may return **null** because, I can't guarantee location has been changed immmediately 
    }

上面,您调用方法 createLocationRequest()来创建位置请求。方法 createLocationRequest()如下所示。

protected void createLocationRequest() {
        //remove location updates so that it resets
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); //Import should not be **android.Location.LocationListener**
    //import should be **import com.google.android.gms.location.LocationListener**;

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        //restart location updates with the new interval
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    }

3)现在,在LocationListener接口的onLocationChange()回调中,您将获得新位置。

@Override
    public void onLocationChanged(Location location) {
        Log.i(TAG, "Location Changed!");
        Log.i(TAG, " Location: " + location); //I guarantee,I get the changed location here

    }

您可以在Logcat中获得如下结果: 03-22 18:34:17.336 817-817 / com.LiveEarthquakesAlerts I / LocationTracker:位置:位置[fuse 37.421998,-122.084000 acc = 20 et = + 15m35s840ms alt = 0.0]

为了能够执行这三个步骤,您应该已经配置了build.gradle,如下所示:

 compile 'com.google.android.gms:play-services-location:10.2.1'