gpt4 book ai didi

android - 使用 LocationListener 每 100 米更新一次位置

转载 作者:太空狗 更新时间:2023-10-29 14:56:27 24 4
gpt4 key购买 nike

我是安卓新手。我正在尝试一个代码,每 100 米获取一次更新,即每 100 米需要在我的应用程序 TextView 上更新位置。

这段代码是否足够,因为当我开车或骑自行车时,TetView 没有得到正确更新。(正确地在我得到相同位置的意义上,我需要关闭应用程序并重新打开它以获取新位置)

还有一个疑问是,在文件中写下最后知道的位置然后再显示它是一种好习惯吗??如果我没有网络连接,我可以获取最后的已知位置。

我的代码:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {


public static final String TAG=MainActivity.class.getSimpleName();
UserLocationDetails locationDetails=new UserLocationDetails();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
TextView userLocationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userLocationView=(TextView) findViewById(R.id.idLocationOfUser);

mGoogleApiClient=new GoogleApiClient.Builder(this).
addConnectionCallbacks(this).
addOnConnectionFailedListener(this).
addApi(LocationServices.API).build();
mLocationRequest = LocationRequest.create().setSmallestDisplacement(100)
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}

@Override
protected void onPause() {
super.onPause();
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}

@Override
protected void onRestart() {
super.onRestart();
}

@Override
protected void onStart() {
super.onStart();
}

@Override
public void onConnected(Bundle bundle) {
Log.i(TAG,"Location Service Connected");
Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Log.e(TAG,"Location is NULL");
}
else {
handleNewLocation(location);
}


}

private void handleNewLocation(Location location) {

double latitude=location.getLatitude();
double longitude=location.getLongitude();
Log.i(TAG,"Latitude : "+location.getLatitude());
Log.i(TAG,"Longitde : "+location.getLongitude());

Log.i(TAG, "Location at : " + location.toString());

locationDetails.setLocationDetailsOfUser(location.toString());

userLocationView.setText(locationDetails.getLocationDetailsOfUser());
}

@Override
public void onConnectionSuspended(int i) {
Log.i(TAG,"Location Service Suspended");

}

@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);
}
}

最佳答案

@Override
public void onLocationChanged(Location location) {
if (location.distanceTo(mCurrentLocation) > 100)
{
// do update stuff
mCurrentLocation = location;
}
}

其中 mCurrentLocation 是最后一个已知位置。

你不能像这样根据距离请求位置更新。

关于android - 使用 LocationListener 每 100 米更新一次位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30986513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com