gpt4 book ai didi

仅来自 wifi 的 Android 位置

转载 作者:搜寻专家 更新时间:2023-11-01 08:45:56 24 4
gpt4 key购买 nike

我有一个运行 Android 4.4.4 的自定义硬件。它有 wifi 连接,但没有 GPS 或蜂窝模块。我只需要根据 wifi 来确定位置,但 Android 似乎不能。在浏览器中访问 google.com 也会显示“未知”位置。虽然 wifi 工作正常 - 浏览互联网等。

这是我用来确定位置的示例类:

public class LocationServices {
Context mContext;
private static final String TAG = LocationServices.class.getSimpleName();

LocationServices(Context c){
mContext = c;
}

/** Check if we can get our location */
public void checkLocation(){
// Get the location manager
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
String locationProviderWifi = LocationManager.NETWORK_PROVIDER;
String locationProviderGPS = LocationManager.GPS_PROVIDER;
LocationListener locationListenerNetwork;
LocationListener locationListenerGPS;
boolean gps_enabled=false;
boolean network_enabled=false;

//check wifi
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
Log.d(TAG,"Wifi connected");
}

//check gps and wifi availability
try{
gps_enabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch(Exception ex){}

try{
network_enabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch(Exception ex){}

if(!gps_enabled){
Log.d(TAG,"GPS: Missing");
}
if(!network_enabled){
Log.d(TAG,"Network: Missing");
}

/* Location change listeners */
try{
// Define a listener that responds to gps location updates
locationListenerGPS = new LocationListener() {
public void onLocationChanged(Location location) {
Log.d(TAG,"GPS: Latitude: " + location.getLatitude() + ", Longitude = " + location.getLongitude());
}

public void onStatusChanged(String provider, int status, Bundle extras) {Log.d(TAG,"location found 1");}

public void onProviderEnabled(String provider) {Log.d(TAG,"location found 2");}

public void onProviderDisabled(String provider) {Log.d(TAG,"location found 3");}
};

locationManager.requestLocationUpdates(locationProviderGPS, 0, 0, locationListenerGPS);
}catch(Exception e){
Log.e(TAG, "Location Exception: " + e.getMessage());
}

try{
// Define a listener that responds to wifi location updates
locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
Log.d(TAG,"NW: Latitude: " + location.getLatitude() + ", Longitude = " + location.getLongitude());
}

public void onStatusChanged(String provider, int status, Bundle extras) {Log.d(TAG,"location found 1");}

public void onProviderEnabled(String provider) {Log.d(TAG,"location found 2");}

public void onProviderDisabled(String provider) {Log.d(TAG,"location found 3");}
};

locationManager.requestLocationUpdates(locationProviderWifi, 0, 0, locationListenerNetwork);
}catch(Exception e){
Log.e(TAG, "Location Exception: " + e.getMessage());
}
}
}

和 logcat 输出:

02-02 18:09:27.009: D/LocationServices(3972): Wifi connected
02-02 18:09:27.009: D/LocationServices(3972): GPS: Missing
02-02 18:09:27.009: D/LocationServices(3972): Network: Missing
02-02 18:09:27.016: E/LocationServices(3972): Location Exception: provider doesn't exist: gps
02-02 18:09:27.016: E/LocationServices(3972): Location Exception: provider doesn't exist: network

list 文件中的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

有没有办法确定这个配置中的位置?

最佳答案

欢迎您尝试寻找一些 Web 服务,这些服务将提供基于公共(public) IP 地址查找的地理位置。准确度范围从“正确的城镇”到“正确的星球”,具体取决于网络配置(例如 VPN)、ISP 等。

关于仅来自 wifi 的 Android 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com