如何获得gps定位android

时间:2011-08-23 08:02:50

标签: android gps

我正在尝试使用一个常量的gps监听器,每隔x分钟将其位置(长和lat坐标)发送到Web服务器。在按钮上单击它还会将其位置发送到Web服务器。我知道要获取gps信号,你可以输入寻找位置的频率,但是如何编写一个程序可以获得gps位置并每x分钟发送一次坐标(即使在背景中也没有按下按钮) ?

//点击

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, whatshouldIputhere?, 0, this);

public void onLocationChanged(Location location) {
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
    }
}

5 个答案:

答案 0 :(得分:18)

我有这个工作:

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}

这很简单。它获得了最佳可用提供商并获得其最后的已知位置。
如果只想使用GPS,请尝试this。 希望有所帮助!

EDITED:

试试这个:

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    LocationListener loc_listener = new LocationListener() {

        public void onLocationChanged(Location l) {}

        public void onProviderEnabled(String p) {}

        public void onProviderDisabled(String p) {}

        public void onStatusChanged(String p, int status, Bundle extras) {}
    };
    locationManager
            .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
    location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}

此代码获取最后一个已知位置,然后请求实际位置。

答案 1 :(得分:3)

一种方法是使用TimerTimerTask

Timer timer = new Timer();
timer.scheduleAtFixedRate(new SendLocationTask(), 0, 60000);

class SendLocationTask extends TimerTask{
    public abstract void run(){
        // send position info here
    }
}

答案 2 :(得分:2)

您可以使用各种方法。您可以使用等待x分钟的单独thread,然后将最新的已知位置发送到服务器。或者您使用的Service或多或少相同。作为第三种可能性,您还可以使用Handler

答案 3 :(得分:2)

您可以创建一个将在后台运行的线程,并且每隔x分钟通过调用一个函数来获取实际位置(请注意,您要创建一个获取x,y坐标的函数,因为您将使用该函数按钮点击aswel)。对于您发布的代码:

locationManager.requestUpdates(provider, minTime, minDistance, intent);

这意味着你的应用程序会每隔x分钟向gps模块发送一个请求以获取whatshouldIputhere吗?

祝你好运, Arkde

答案 4 :(得分:0)

此代码适用于我(api 17)

try {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
    {
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    lat = location.getLatitude();
        lon = location.getLongitude();


    }
}
catch (Exception e){
    e.printStackTrace();
}