Android 仅来自 wifi 的位置

Android location from wifi only

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

这是我的示例 class 以确定位置:

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

清单文件中的权限:

<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" />

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

欢迎您尝试寻找一些可提供基于 public IP 地址查找的地理位置的 Web 服务。准确度范围从 "correct town" 到 "correct planet",具体取决于网络配置(例如 VPN)、ISP 等。