+-
android – 使用GPS和WIFI获取当前位置
我目前只使用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);
}
最佳答案
您正在使用locationmanagers getbestprovider方法,该方法只是获取最符合您条件的任何位置提供者.
如果您想获得特定的位置提供程序,请使用getProvider方法.该参数应该是getProviders方法的结果之一.
一些链接:
http://developer.android.com/guide/topics/location/obtaining-user-location.html
http://developer.android.com/reference/android/location/LocationManager.html
点击查看更多相关文章

转载注明原文:android – 使用GPS和WIFI获取当前位置 - 乐贴网