通过 GPS 和 WIFI 获取当前位置

发布于 2024-11-05 13:50:00 字数 1266 浏览 6 评论 0原文

我目前仅使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-11-12 13:50:00

您正在使用 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

滿滿的愛 2024-11-12 13:50:00

如果您想从特定提供商处获取更新,您可以使用
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 of LocationManager. A list of available providers can be obtained using the getProviders(boolean enabledOnly) method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文