6

我有这段代码用于定位地图应用程序。我在 Galaxy S2 设备中对其进行了测试并且工作正常,但是当我在 nexus 4 设备中对其进行测试时,onLocationChanged回调永远不会被调用。我可能做错了什么?

这是我的活动代码:

    public class BaseMapActivity extends FragmentActivity implements Injectable,
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

    private MapEntity component;
    private boolean yetStarted = false;

    private LocationClient mLocationClient;
    private LocationRequest mLocationRequest;
    private Location location;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map_activity);
        mLocationClient = new LocationClient(this, this, this);
        mLocationRequest = LocationRequest.create();

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // TODO
        mLocationRequest.setInterval(5000); // TODO
        mLocationRequest.setFastestInterval(5000); // TODO

        replaceFragment(R.id.map_container, mapFragment);

        // Agrega el fragment de extras
        Fragment extrasFragment = component.getExtrasFragment();
        replaceFragment(R.id.map_extras, extrasFragment);
    }

    @Override
    protected void onStart() {
        super.onStart();

            mLocationClient.connect();
    }

    @Override
    protected void onStop() {

        if (mLocationClient.isConnected()) {
            mLocationClient.removeLocationUpdates(this);
            mLocationClient.disconnect();
        }

        super.onStop();
    }

    @Override
    public void onConnected(Bundle arg0) {

        mLocationClient.requestLocationUpdates(mLocationRequest, this);
        location = mLocationClient.getLastLocation();

        if (location == null) {
            return;
        }

        Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDisconnected() {
        mLocationClient.removeLocationUpdates(this);
        Toast.makeText(this, "onDisconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location location) {
        component.setCenter(location);
        String msg = "Updated Location: "
                + Double.toString(location.getLatitude()) + ","
                + Double.toString(location.getLongitude());
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {

        case CONNECTION_FAILURE_RESOLUTION_REQUEST:

            switch (resultCode) {
            case Activity.RESULT_OK:
                break;
            }
        }
    }

    @SuppressLint("ShowToast")
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "An error ocurred getting current location",
                    Toast.LENGTH_SHORT);
        }

    }

}

奇怪的是它在一个设备上工作,但在另一个设备上却不行。

4

1 回答 1

0

尝试设置位置模拟以查看是否调用了 locationChange。

在这里,您有一个我在生产前用于此的示例。

private Location createMockLocation() {
    if (mockLocation == null) {
        mockLocation = new Location("mock");
        mockLocation.setLatitude(41.4934133);
        mockLocation.setLongitude(-23.8729252);
        mockLocation.setAccuracy(1.0f);
        mockLocation.setTime(new Date().getTime());
    } else {
        mockLocation.setLatitude(mockLocation.getLatitude() + 0.00003);
        mockLocation.setLongitude(mockLocation.getLongitude() + 0.00001);
        mockLocation.setTime(new Date().getTime());
    }
    return mockLocation;
}

private void mockLocation() {
    LocationServices.FusedLocationApi.setMockMode(clientApi, true);
    LocationServices.FusedLocationApi.setMockLocation(clientApi, createMockLocation());

    final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Runnable() {
        public void run() {
            LocationServices.FusedLocationApi.setMockLocation(clientApi, createMockLocation());
            // Log.d("MockLocation",
            // "mockLocation: Lat:"+mockLocation.getLatitude()+" Long:"+mockLocation.getLongitude());
        }
    }, 0, NORMAL_INTERVAL, TimeUnit.MILLISECONDS);
}

使用此方法,您可以在 ms 中获得每个 NORMAL_INTERVAL 的更新位置。

希望这可以帮助 :)。

于 2015-04-07T12:00:23.633 回答