问题描述
我想有一个恒定的GPS监听器,将发送其位置(长和纬度坐标)发送到Web服务器每隔x分钟。在一个按钮,单击它也将发送其位置到Web服务器。我认识到,得到的GPS信号,你键入多久找到一个位置,但我怎么写一个程序,可以得到GPS的位置,并发送其坐标每x分钟(甚至在后台的时候不是通过一个按钮preSS?
//在上点击
LocationManager LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,whatshouldIputhere?0,这一点);
和
公共无效onLocationChanged(位置定位){
如果(位置!= NULL){
双纬度= location.getLatitude();
双LNG = location.getLongitude();
}
}
我有这个工作:
私人无效_getLocation(){
//获取位置管理器
LocationManager locationManager =(LocationManager)
getSystemService(LOCATION_SERVICE);
标准标准=新标准();
字符串bestProvider = locationManager.getBestProvider(标准,FALSE);
位置位置= locationManager.getLastKnownLocation(bestProvider);
尝试 {
纬度= location.getLatitude();
LON = location.getLongitude();
}赶上(NullPointerException异常E){
土地增值税= -1.0;
LON = -1.0;
}
}
这很简单。它得到的最好的供应商,并获得其最后已知位置。
如果你只用GPS想要的话,尝试这。
希望它可以帮助!
编辑:
试试这个:
私人无效_getLocation(){
//获取位置管理器
LocationManager locationManager =(LocationManager)
getSystemService(LOCATION_SERVICE);
标准标准=新标准();
字符串bestProvider = locationManager.getBestProvider(标准,FALSE);
位置位置= locationManager.getLastKnownLocation(bestProvider);
LocationListener的loc_listener =新LocationListener的(){
公共无效onLocationChanged(位置L){}
公共无效onProviderEnabled(串P){}
公共无效onProviderDisabled(串P){}
公共无效onStatusChanged(串P,INT地位,捆绑演员){}
};
locationManager
.requestLocationUpdates(bestProvider,0,0,loc_listener);
位置= locationManager.getLastKnownLocation(bestProvider);
尝试 {
纬度= location.getLatitude();
LON = location.getLongitude();
}赶上(NullPointerException异常E){
土地增值税= -1.0;
LON = -1.0;
}
}
这code得到最后已知位置,然后做实际位置的请求。
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?
//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.
Hope it helps!
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.
这篇关于如何获得GPS定位的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!