您好我正在使用谷歌地图LocationListener
。我可以使用 Fused API
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000 * 60 * 1);
mLocationRequest.setFastestInterval(1000 * 60 * 1);
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
在这里我绘制路径:
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
routePoints.add(latLng);
Polyline route = mGoogleMap.addPolyline(new PolylineOptions()
.width(5)
.color(Color.BLUE)
.geodesic(false)
.zIndex(3));
route.setPoints(routePoints);
}
关键是,我需要在用户移动时绘制实时路径,并在用户停止时停止,而不管LocationRequest
的间隔时间。
答案 0 :(得分:4)
尝试此方法,以便根据需要决定和更改值。调用此方法需要多长时间间隔和最小距离。没有互联网的工作。
private LocationManager locationManager;
private android.location.LocationListener myLocationListener;
public void checkLocation() {
String serviceString = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(serviceString);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
myLocationListener = new android.location.LocationListener() {
public void onLocationChanged(Location locationListener) {
if (isGPSEnabled(YourActivityName.this)) {
if (locationListener != null) {
if (ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
} else if (isInternetConnected(YourActivityName.this)) {
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener); // here the min time interval and min distance
}
isInternetConnected方法
public static boolean isInternetConnected(Context ctx) {
ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Check if wifi or mobile network is available or not. If any of them is
// available or connected then it will return true, otherwise false;
if (wifi != null) {
if (wifi.isConnected()) {
return true;
}
}
if (mobile != null) {
if (mobile.isConnected()) {
return true;
}
}
return false;
}
isGpsEnabled方法
public boolean isGPSEnabled(Context mContext) {
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
逻辑:通过保留变量来检查lastlocation
是currantLocation
,如果是,则表示如果没有绘制路径则不会被移动
答案 1 :(得分:0)
在你的课程中定义google api的这种方法:
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
// make your code to draw path , and refresh the map to not hve a ot of path .
}
有关详细信息,请参阅here