gpt4 book ai didi

android - 如何获得 5 分钟 android 一次的 gps 位置?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:55 26 4
gpt4 key购买 nike

嗨,谁能给我一个示例代码,让我每五分钟获取一次位置,我已经尝试过了,我可以通过点击按钮获取一次位置,但我需要它显示一次,持续五分钟。

谢谢

这是我的代码:

public void checkLocation(View v) {

//initialize location manager
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

//check if GPS is enabled
//if not, notify user with a toast
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS is disabled.", Toast.LENGTH_SHORT).show();
} else {

//get a location provider from location manager
//empty criteria searches through all providers and returns the best one
String providerName = manager.getBestProvider(new Criteria(), true);
Location location = manager.getLastKnownLocation(providerName);

TextView tv = (TextView)findViewById(R.id.locationResults);
if (location != null) {
tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude");
} else {
tv.setText("Last known location not found. Waiting for updated location...");
}
//sign up to be notified of location updates every 15 seconds - for production code this should be at least a minute
manager.requestLocationUpdates(providerName, 15000, 1, this);
}
}

@Override
public void onLocationChanged(Location location) {
TextView tv = (TextView)findViewById(R.id.locationResults);
if (location != null) {
tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude");
} else {
tv.setText("Problem getting location");
}
}

@Override
public void onProviderDisabled(String arg0) {}

@Override
public void onProviderEnabled(String arg0) {}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

// Find the closest Bart Station
public String findClosestBart(Location loc) {
double lat = loc.getLatitude();
double lon = loc.getLongitude();

double curStatLat = 0;
double curStatLon = 0;
double shortestDistSoFar = Double.POSITIVE_INFINITY;
double curDist;
String curStat = null;
String closestStat = null;

//sort through all the stations
// write some sort of for loop using the API.

curDist = Math.sqrt( ((lat - curStatLat) * (lat - curStatLat)) +
((lon - curStatLon) * (lon - curStatLon)) );
if (curDist < shortestDistSoFar) {
closestStat = curStat;
}

return closestStat;

}

最佳答案

这是获取位置的代码,并设置 gps 的监听器以在几分钟和距离内获取当前位置,我还使用可运行对象每隔几分钟获取一次位置。

Location gpslocation = null;

private static final int GPS_TIME_INTERVAL = 60000; // get gps location every 1 min
private static final int GPS_DISTANCE= 1000; // set the distance value in meter

/*
for frequently getting current position then above object value set to 0 for both you will get continues location but it drown the battery
*/

private void obtainLocation(){
if(locMan==null)
locMan = (LocationManager) getSystemService(LOCATION_SERVICE);

if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(isLocationListener){
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
GPS_TIME_INTERVAL, GPS_DISTANCE, GPSListener);
}
}
}
}

现在使用此方法获取当前位置,并在每 1 分钟和 1000 米距离发生位置变化时调用监听器。

为了每 5 分钟获取一次,您可以使用此处理程序和 runnable 在设置好的时间段内获取此位置:

private static final int HANDLER_DELAY = 1000*60*5;

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
myLocation = obtainLocation();
handler.postDelayed(this, HANDLER_DELAY);
}
}, START_HANDLER_DELAY);

这是位置更改事件的 GPS 监听器:

private LocationListener GPSListener = new LocationListener(){
public void onLocationChanged(Location location) {
// update location
locMan.removeUpdates(GPSListener); // remove this listener
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

您可以为监听器和处理程序设置相同的间隔时间以获取 GPS 位置。

关于android - 如何获得 5 分钟 android 一次的 gps 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16055447/

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