Android 从未调用LocationServices.FusedLocationApi.RequestLocationUpdate

Android 从未调用LocationServices.FusedLocationApi.RequestLocationUpdate,android,google-maps,Android,Google Maps,我没有使用LocationManager,而是使用LocationServices方法获取当前gps位置和位置更新 代码如下: public void onConnected(@Nullable Bundle bundle) { mLocationRequest = new LocationRequest();//PRIORITY_BALANCED_POWER_ACCURACY mLocationRequest.setInterval(1000*30*1); mLocat

我没有使用LocationManager,而是使用LocationServices方法获取当前gps位置和位置更新

代码如下:

public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();//PRIORITY_BALANCED_POWER_ACCURACY
    mLocationRequest.setInterval(1000*30*1);
    mLocationRequest.setFastestInterval(1000*30*1);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest,this);
    }
}
public void onLocationChanged(Location location) { mLastLocation = location;
        if (mCurrLocationMarker != null)
        {
            mCurrLocationMarker.remove();
        }
    //Place current location marker
     latLng = new LatLng(location.getLatitude(), location.getLongitude());
     lat= location.getLatitude();
     log=location.getLongitude();
     trim_check_location_change(lat,log);
     MarkerOptions markerOptions = new MarkerOptions();
     markerOptions.position(latLng);
     markerOptions.title(""+s);
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
     mCurrLocationMarker = mMap.addMarker(markerOptions);
     //move map camera
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
     CurrentLocation = latLng;
}

当我从一个地方移动到另一个地方时,蓝色标记也随之移动。
但是onlocationchanged方法从未被调用。

我在这里开发了fused location api演示应用程序和实用程序包

如果对你有用,试试看。要使用融合位置api获取位置,只需编写以下代码段

new LocationHandler(this)
    .setLocationListener(new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        // Get the best known location
    }
}).start();
如果你想定制它,只需在这里找到文档

更新

我已经根据您的需要编写了一个示例代码,试试这个

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;

import in.teramatrix.utilities.service.LocationHandler;
import in.teramatrix.utilities.util.MapUtils;

/**
 * Lets see how to use utilities module by implementing location listener.
 *
 * @author Khan
 */

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener {

    private GoogleMap map;
    private Marker marker;
    private LocationHandler locationHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Obtaining an instance of map
        FragmentManager manager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) manager.findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        this.locationHandler = new LocationHandler(this)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(5000)
                .setFastestInterval(10000)
                .setLocationListener(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        this.map = map;
        this.locationHandler.start();
    }

    @Override
    public void onLocationChanged(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        if (marker == null) {
            marker = MapUtils.addMarker(map, latLng, R.drawable.ic_current_location);
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14), 500, null);
        } else {
            marker.setPosition(latLng);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (locationHandler != null) {
            locationHandler.stop();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LocationHandler.REQUEST_LOCATION) {
            locationHandler.start();
        }
    }
}

希望它能帮助您。

我检查了您的github代码。我把距离改为1mtr,移动了几米。我用祝酒词来表示位置的改变。但是onLocatioChanged仍然不起作用。@cgeek GPS将无法识别仅几米的距离。出去看看。它应该至少10-20米。包括你的github代码。工作得很好。。非常感谢。