【发布时间】:2011-05-06 08:00:45
【问题描述】:
我目前仅使用 GPS 来获取用户的当前位置以返回某些结果。就像在比利时一样,如果你在里面,大部分时间你都无法获得 GPS 连接。所以我想通过wifi连接获取当前位置。
我找到了这个例子:get the current location (gps/wifi) 但是
我不确定哪些线路告诉设备选择哪个连接。我猜是这个:String provider = myLocationManager.getBestProvider(criteria,true); 我必须添加到我的代码中。
当我检查其中的内容时,我总是得到一个空值,我不太明白为什么。
我的代码目前看起来像这样:
public void getOwnLngLat(){
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
final LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
longitude="" + location.getLongitude();
latitude="" + location.getLatitude();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), 2000, 10, locationListener);
}
【问题讨论】: