[英]how to get gps location android
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. 我试图有一个恒定的gps监听器,每隔x分钟将其位置(长和lat坐标)发送到Web服务器。 On a button click it will also send its location to the webserver. 在按钮上单击它还会将其位置发送到Web服务器。 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信号,你可以输入寻找位置的频率,但是如何编写一个程序可以获得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();
}
}
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 . 如果您只想使用GPS,请试试这个 。
Hope it helps! 希望能帮助到你!
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. 此代码获取最后的已知位置,然后请求实际位置。
One method is to use Timer
and TimerTask
一种方法是使用Timer
和TimerTask
Timer timer = new Timer();
timer.scheduleAtFixedRate(new SendLocationTask(), 0, 60000);
class SendLocationTask extends TimerTask{
public abstract void run(){
// send position info here
}
}
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. 您可以使用等待x分钟的单独线程 ,然后将最新的已知位置发送到服务器。 Or you use a Service that does more or less the same. 或者您使用的服务或多或少相同。 As a third possibility you could also use a Handler . 作为第三种可能性,您也可以使用Handler 。
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). 您可以创建一个将在后台运行的线程,并且每隔x分钟通过调用一个函数来获取实际位置(请注意,您希望创建一个获取x,y坐标的函数,因为您将使用该按钮单击还有)。 For the code you posted : 对于您发布的代码:
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模块发送一个请求以获取whatshouldIputhere?
Good luck, Arkde 祝你好运,Arkde
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.