[英]GoogleApiClient doesn't refresh my location at onChangedLocation method
我想通过刷新位置来更新代码。 目前我一个代码:
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
但是我很确定它不会刷新我的位置。 我以为在应用运行的情况下驾驶汽车,而我的位置没有改变。 请告诉我我应该解决什么以改善它? 我了解它跟随我的位置的事情是,每当我更改位置并走出地图时,地图就会跟随我。
还请告诉我是否有可能创建一种图标来显示我当前的位置( 不是标记 -这就是我现在拥有的图标)。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleMap mMap;
private GPSTracker gpsTracker;
private Location location;
private double lat, lng;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private LinearLayoutManager linearLayoutManager;
private List<WashLocation> washLocations;
private Button favoriteWashesButton;
private Button isFavorite;
List<String> spinnerArray = new ArrayList<String>();
Spinner sItems;
String choosenDistance;
private static DataBaseHelper dataBaseHelper;
AlertDialog alert;
private LocationManager locationManager;
private LatLng myLocation;
private Location lastKnownLocation;
/////////////////////////////////////////////
private GoogleApiClient mGoogleApiClient;
public static final String TAG = MapsActivity.class.getSimpleName();
private LocationRequest mLocationRequest;
private LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
dataBaseHelper = new DataBaseHelper(getApplicationContext());
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
mGoogleApiClient.connect();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMapAsync(this);
if (mMap != null) {
//setUpMap();
}
}
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
public LatLng getLatLng() {
return latLng;
}
public void setLatLng(LatLng latLng) {
this.latLng = latLng;
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
final LatLng latLng = new LatLng(currentLatitude, currentLongitude);
setLatLng(latLng);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11), 2000, null);
generateMap(getLatLng(), "3000");
sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
washLocations.clear();
washLocations.size();
String latitude = String.valueOf(latLng.latitude);
String longitude = String.valueOf(latLng.longitude);
generateMap(getLatLng(), null);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private String getCity(String s) {
int i = s.lastIndexOf(',');
return s.substring(i, s.length());
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location);
}
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Location services suspended. Please reconnect.");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}
}
您可以使用融合的位置服务获取位置更新。 进行后台服务并在每个特定间隔获取位置。在这里查看我的答案https://stackoverflow.com/a/43602323/3789993
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.