Rad*_*dwa 4 android google-maps
你好,我正在使用谷歌地图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);
Run Code Online (Sandbox Code Playgroud)
我在这里绘制路径:
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);
}
Run Code Online (Sandbox Code Playgroud)
关键是,我需要在用户移动时绘制实时路径,并在用户停止时停止,而不管LocationRequest.
试试这个方法,这样你就可以根据需要决定和更改值。调用这个方法需要多少时间间隔和多少最小距离。也可以在没有互联网的情况下工作。
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
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
isGpsEnabled 方法
public boolean isGPSEnabled(Context mContext) {
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
Run Code Online (Sandbox Code Playgroud)
逻辑:检查lastlocation是currantLocation通过保留变量,如果是,则表示如果不绘制路径,则不会移动