仅限wifi的Android位置

时间:2015-02-02 18:18:04

标签: android gps location android-wifi

我是运行Android 4.4.4的自定义硬件。它有一个wifi连接,但没有GPS或蜂窝模块。我需要仅根据wifi确定位置,但似乎Android无法实现。在浏览器中访问google.com也会显示' unknown'地点。无线网络工作正常 - 浏览互联网等。

这是我的样本类来确定位置:

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

无论如何确定此配置中的位置?

1 个答案:

答案 0 :(得分:3)

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