如何获得gps位置一次5分钟android? [英] How to get gps location once for 5 mins android?

查看:118
本文介绍了如何获得gps位置一次5分钟android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好可以有任何人给我一个示例代码获取每五分钟的位置,请我已经尝试过,我可以通过点击按钮
获得位置,但我需要它显示一次五分钟。



谢谢

这是我的代码:



<
$初始化位置管理器
manager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

//检查GPS是否启用
//如果不是,通知用户使用敬酒
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this,GPS is disabled。,Toast.LENGTH_SHORT).show();
} else {

//从位置管理器获取位置提供者
//空标准搜索所有提供者并返回最佳值$ b $ String String providerName = manager。 getBestProvider(new Criteria(),true);
位置位置= manager.getLastKnownLocation(providerName);

TextView tv =(TextView)findViewById(R.id.locationResults);
if(location!= null){
tv.setText(location.getLatitude()+latitude,+ location.getLongitude()+longitude);
} else {
tv.setText(找到最后找到的位置,等待更新的位置...);
}
//注册每隔15秒通知位置更新 - 对于产品代码,这应该至少是一分钟
manager.requestLocationUpdates(providerName,15000,1,this);


$ b @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){}
$ b $ @覆盖
public void onProviderEnabled (String arg0){}

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

//找到最近的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;

//对所有工作站进行排序
//使用API​​编写某种for循环。

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

return closestStat;


$ / code $ / pre

解决方案

获取位置代码并设置gps侦听器以在几分钟和几分钟内获取当前位置的代码,我也使用了runnable对象来每隔几分钟获取一次位置。

 位置gpslocation = null; 

private static final int GPS_TIME_INTERVAL = 60000; //每1分钟获得一次GPS位置
private static final int GPS_DISTANCE = 1000; //设置米的距离值

/ *
用于频繁获取当前位置,然后将高于对象值的值设置为0,这样您将获得持续位置,但会淹死电池
* /

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






$ b现在使用此方法获取当前位置,并且每隔1分钟和1000米的距离就会调用位置更改的监听器。



每隔5分钟就可以使用这个处理程序和可运行以在设置周期时间获得此位置:

  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){
//更新位置
locMan.removeUpdates(GPSListener ); //删除这个监听器

$ b $ public void onProviderDisabled(String provider){
}

public void onProviderEnabled(String provider){
}

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

您可以设置侦听程序和处理程序的间隔时间,以获取GPS位置。


hi there can anybody give me a sample code for get location for every five minutes please i have tried and i can get location once by cliking on button, but i need it to be displayed once for five minutes.

thank you

this is my code :

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;

        }   

解决方案

Here is the code for getting location and set the listener for gps to get current location on few minute and distance, also I have used runnable object to get the location on every few minutes.

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

Now use this method to get the current location and the listener was called on location change with every 1 min and 1000 meter of distance.

For getting every 5 min you can use this handler and runnable to get this location on well set period time:

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

Here is GPS listener for location change event:

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

You can set interval time for listener and handler same for getting GPS location.

这篇关于如何获得gps位置一次5分钟android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆