我想通过刷新我的位置来更新我的代码。目前我是一个代码:
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);
}
}
答案 0 :(得分:0)
您可以使用融合位置服务来获取位置更新。提供后台服务,并在每个特定的时间间隔内获取位置。请在https://stackoverflow.com/a/43602323/3789993
查看我的答案