如何获得gps定位android

时间:2023-02-05 18:20:19

I am trying to have a constant gps listener that will send its location (long and lat coordinates) to a web server every x mins. On a button click it will also send its location to the webserver. I realize that to get the gps signal you type in how often to find a position, but how do I write a program that can get the gps location and send its coordinates every x mins (even in the background when not and by a button press?

我正在尝试拥有一个固定的gps监听器,它将每x分钟将其位置(long and lat坐标)发送到web服务器。点击一个按钮,它也会将它的位置发送到webserver。我意识到,要得到gps信号,你需要多久输入一次才能找到一个位置,但我该如何编写一个程序来获取gps位置,并每隔x分钟发送一次它的坐标(即使是在不需要的背景下,也要按下按钮)?

//in the on click

/ /点击

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

and

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

5 个解决方案

#1


18  

I've got this working:

我有这个工作:

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

It's simple. It gets the best available provider and gets its last known position.
If you want it only with the GPS, try this.

Hope it helps!

这很简单。它获得了最好的可用提供者,并获得了最后的已知位置。如果你只想用GPS,试试这个。希望它可以帮助!

EDITED:

try this:

试试这个:

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

This code gets the last known location and then do a request for the actual location.

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

#2


3  

One method is to use Timer and TimerTask

一种方法是使用定时器和TimerTask。

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

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

#3


2  

There are various methods you could use. You could use a separate thread that waits for x minutes and then sends the latest known location to the server. Or you use a Service that does more or less the same. As a third possibility you could also use a Handler.

您可以使用各种方法。您可以使用一个单独的线程,等待x分钟,然后将最新的已知位置发送到服务器。或者您使用的服务或多或少都是相同的。作为第三种可能性,您还可以使用一个处理程序。

#4


2  

You can create a thread that will run in background and every x minutes you get the actual position by calling a function that does that (Note that you want to make a function that get the x,y coordinates since you will use that on button click aswel). For the code you posted :

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

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

That means that your application will send a request to gps module every x min for the whatshouldIputhere?

这意味着您的应用程序将在每x分钟向gps模块发送一个请求,用于什么?

Good luck, Arkde

祝你好运,Arkde

#5


0  

this code works for me (api 17)

此代码适用于我(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();
}

#1


18  

I've got this working:

我有这个工作:

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

It's simple. It gets the best available provider and gets its last known position.
If you want it only with the GPS, try this.

Hope it helps!

这很简单。它获得了最好的可用提供者,并获得了最后的已知位置。如果你只想用GPS,试试这个。希望它可以帮助!

EDITED:

try this:

试试这个:

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

This code gets the last known location and then do a request for the actual location.

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

#2


3  

One method is to use Timer and TimerTask

一种方法是使用定时器和TimerTask。

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

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

#3


2  

There are various methods you could use. You could use a separate thread that waits for x minutes and then sends the latest known location to the server. Or you use a Service that does more or less the same. As a third possibility you could also use a Handler.

您可以使用各种方法。您可以使用一个单独的线程,等待x分钟,然后将最新的已知位置发送到服务器。或者您使用的服务或多或少都是相同的。作为第三种可能性,您还可以使用一个处理程序。

#4


2  

You can create a thread that will run in background and every x minutes you get the actual position by calling a function that does that (Note that you want to make a function that get the x,y coordinates since you will use that on button click aswel). For the code you posted :

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

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

That means that your application will send a request to gps module every x min for the whatshouldIputhere?

这意味着您的应用程序将在每x分钟向gps模块发送一个请求,用于什么?

Good luck, Arkde

祝你好运,Arkde

#5


0  

this code works for me (api 17)

此代码适用于我(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();
}