通过 GPS 和 WIFI 获取当前位置
我目前仅使用 GPS 来获取用户的当前位置以返回某些结果。就像在比利时一样,如果您在室内,大多数时候您无法获得 GPS 连接。所以我想通过 wifi 连接获取当前位置。
我找到了这个例子:获取当前位置(gps/wifi) 但
我不确定哪些线路告诉设备选择哪个连接。我猜是这个: Stringprovider = 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);
}
I'm currently using GPS only to get current location of the user to return certain results. As in Belgium, if you're inside, most of the time you can't get GPS-connection. So I want to get the current location with the wifi-connection.
I've found this example: get the current location (gps/wifi)
But
I'm not sure which lines tell the device which connection to choose. I'm guessing it's this one: String provider = myLocationManager.getBestProvider(criteria,true);
That I've to add to my code.
When I check what's inside of this, I always get a null-value, I don't quite understand why.
My code looks currently like this:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用 locationmanagers getbestprovider 方法,该方法只需获取最符合您的标准的任何位置提供程序。
如果您想获取特定的位置提供程序,请使用 getProvider 方法。该参数应该是 getProviders 方法的结果之一。
一些链接:
http://developer.android.com/guide/topics/location /获取-用户-位置.html
http://developer.android.com/reference/android/location/LocationManager.html
you are using locationmanagers getbestprovider method which simply gets any location provider which most closely matches your criteria.
if you want to get a specific location provider then use the getProvider method. the parameter should be one of the results of the getProviders method.
some links:
http://developer.android.com/guide/topics/location/obtaining-user-location.html
http://developer.android.com/reference/android/location/LocationManager.html
如果您想从特定提供商处获取更新,您可以使用
requestLocationUpdates(Stringprovider,longminTime,floatminDistance,LocationListenerlistener)
方法位置管理器
。可以使用获取可用提供程序的列表getProviders(booleanenabledOnly)
方法。If you want to get updates from a specific provider you can use the
requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
method ofLocationManager
. A list of available providers can be obtained using thegetProviders(boolean enabledOnly)
method.