在 Lollipop 5.0.2 中,位置返回 0.0,带有 WIFI/3G/4G [英] Location returns 0.0 with WIFI/3G/4G in Lollipop 5.0.2

查看:30
本文介绍了在 Lollipop 5.0.2 中,位置返回 0.0,带有 WIFI/3G/4G的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据 WiFi/3G/4G 连接获取位置,但它总是返回 0.0 作为 纬度和经度.如果与 GPS ON 一起使用相同的代码,那么它可以工作,因此从 4.4 开始会有所更改.

也尝试了以下链接,但它也不起作用.

http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

公共类 GPSTracker 扩展服务实现 LocationListener {私人静态最终字符串标签=GPSTracker";私有最终上下文 mContext;布尔 isGPSEnabled = false;布尔 isNetworkEnabled = false;boolean canGetLocation = false;位置位置;//地点双纬度;//纬度双经度;//经度双倍速度;双高度;双轴承;//更改更新的最小距离,以米为单位私有静态最终长 MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;//10 米//更新之间的最短时间,以毫秒为单位私有静态最终长 MIN_TIME_BW_UPDATES = 5000;//10 秒受保护的位置管理器位置管理器;公共 GPSTracker(上下文上下文){this.mContext = 上下文;获取位置();}公共位置 getLocation() {尝试 {如果(mContext == null)Log.d("GPS 追踪器", "上下文为空");locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);//获取 GPS 状态isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);//获取网络状态isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);if (!isGPSEnabled && !isNetworkEnabled) {//没有启用网络提供程序Log.d("GPSTracker", "未启用提供程序");} 别的 {this.canGetLocation = true;//首先从 Network Provider 获取位置如果(isNetworkEnabled){locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这个);Log.d("GPSTracker", "网络启用");如果(位置管理器!= null){位置 = 位置管理器.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);如果(位置!= null){纬度 = location.getLatitude();经度 = location.getLongitude();高度 = location.getAltitude();速度 = location.getSpeed();轴承 = location.getBearing();}}}//如果启用 GPS 获取纬度/经度使用 GPS 服务if (isGPSEnabled) {如果(位置==空){locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这个);Log.d("GPSTracker", "GPS 启用");如果(位置管理器!= null){位置 = 位置管理器.getLastKnownLocation(LocationManager.GPS_PROVIDER);如果(位置!= null){纬度 = location.getLatitude();经度 = location.getLongitude();高度 = location.getAltitude();速度 = location.getSpeed();轴承 = location.getBearing();}}}}}} 捕捉(异常 e){e.printStackTrace();}返回位置;}/*** 停止使用 GPS 侦听器调用此函数将停止在您的系统中使用 GPS* 应用程序* */公共无效停止使用GPS(){如果(位置管理器!= null){locationManager.removeUpdates(GPSTracker.this);}}/*** 获取纬度函数* */公共双getLatitude(){如果(位置!= null){纬度 = location.getLatitude();}//返回纬度返回纬度;}/*** 获取经度的函数* */公共双getLongitude(){如果(位置!= null){经度 = location.getLongitude();}//返回经度返回经度;}/***检查GPS/wifi启用的功能** @return 布尔值* */公共布尔canGetLocation(){返回 this.canGetLocation;}@覆盖公共无效onLocationChanged(位置位置){}@覆盖公共无效onProviderDisabled(字符串提供者){}@覆盖公共无效onProviderEnabled(字符串提供者){}@覆盖public void onStatusChanged(String provider, int status, Bundle extras) {}@覆盖公共 IBinder onBind(Intent arg0) {返回空值;}}

是否有任何解决方案可以从 4.4 kitkat 开始基于 WiFi/Data 连接获取位置??

任何帮助/建议将不胜感激.

解决方案

要遵循的步骤:

第1步:检查清单文件中的以下权限

<块引用>

第二步:检查服务类

import android.annotation.SuppressLint;公共类 GPSTracker 扩展服务实现 LocationListener {公共静态布尔 isGPSEnabled = false;公共静态布尔 isNetworkEnabled = false;静态布尔 canGetLocation = false;位置位置;//地点双纬度;//纬度双经度;//经度私有静态最终长 MIN_DISTANCE_CHANGE_FOR_UPDATES = 2;//2米私有静态最终 long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;//1分钟受保护的位置管理器位置管理器;@覆盖公共 IBinder onBind(Intent arg0) {返回空值;}@SuppressWarnings("静态访问")@覆盖公共无效 onCreate() {超级.onCreate();Log.d("onCreate", "成功!");纬度和经度();}公共无效纬度和经度(){locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);if (!isGPSEnabled && !isNetworkEnabled) {Toast.makeText(getApplicationContext(), "未找到提供者!", Toast.LENGTH_SHORT).show();}别的{GPSTracker.canGetLocation = true;如果(启用 GPS){如果(位置==空){locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这个);Log.d("网络", "GPS");如果(位置管理器!= null){Log.d("locationManager", "不为空");位置 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);如果(位置!= null){Log.d("Network", "GPS lat and long gets ");Toast.makeText(getApplicationContext(), "isNetworkEnabled!", Toast.LENGTH_SHORT).show();纬度 = location.getLatitude();经度 = location.getLongitude();Helper.savePreferences(getApplicationContext(), NameConversion.LATITUDE,String.valueOf(latitude));Helper.savePreferences(getApplicationContext(), NameConversion.LONGITUDE,String.valueOf(longitude));}别的{Log.d("location","is getting null");}}}}否则如果(isNetworkEnabled){locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这个);Log.d("网络", "网络");如果(位置管理器!= null){位置 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);如果(位置!= null){//Toast.makeText(getApplicationContext(), "isGPSEnabled!", Toast.LENGTH_SHORT).show();纬度 = location.getLatitude();经度 = location.getLongitude();Helper.savePreferences(getApplicationContext(), NameConversion.LATITUDE,String.valueOf(latitude));Helper.savePreferences(getApplicationContext(), NameConversion.LONGITUDE,String.valueOf(longitude));}}}}}公共位置 getLocation() {返回位置;}/*** 停止使用 GPS 侦听器调用此函数将停止在您的系统中使用 GPS* 应用程序* */公共无效停止使用GPS(){如果(位置管理器!= null){locationManager.removeUpdates(GPSTracker.this);}}/*** 获取纬度函数* */公共双getLatitude(){如果(位置!= null){纬度 = location.getLatitude();}//返回纬度返回纬度;}/*** 获取经度的函数* */公共双getLongitude(){如果(位置!= null){经度 = location.getLongitude();}//返回经度返回经度;}公共无效 sendLocationToServer() {Log.i("sendLocationToServer", "调用!");}@覆盖公共无效 onDestroy() {super.onDestroy();Log.i("onDestroy", "调用!");停止自我();}/***检查GPS/wifi启用的功能** @return 布尔值* */@SuppressWarnings("静态访问")公共布尔canGetLocation(){返回 this.canGetLocation;}/***显示设置警报对话框的功能按下设置按钮将* lauch 设置选项* */@SuppressLint("NewApi")@覆盖公共无效onLocationChanged(位置位置){纬度和经度();}@覆盖公共无效onProviderDisabled(字符串提供者){}@覆盖公共无效onProviderEnabled(字符串提供者){}@覆盖public void onStatusChanged(String provider, int status, Bundle extras) {}}

<块引用>

第 3 步:在 Manifest 文件中启用服务

在应用程序标签内

 <服务android:name=".你的包名.GPSTracker"安卓:启用=真"></服务>

<块引用>

setp 4 : 在你想要的地方启动服务

如果您仍然遇到问题,希望这能奏效,可能是您的手机无法获取 GPS 和网络定位.

I am trying to fetch location based on WiFi/3G/4G connection but it always returns 0.0 as latitude and longitude. If same code is used with GPS ON then it works so something is changed from 4.4 onwards.

Also tried following link but its not working too.

http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

public class GPSTracker extends Service implements LocationListener {

private static final String TAG = "GPSTracker";

private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude
double speed;
double altitude;
double bearings;

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 5000; // 10 seconds

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {

    try {

        if (mContext == null)
            Log.d("GPS Tracker", "Context is null");

        locationManager = (LocationManager) mContext
                .getSystemService(Context.LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled

            Log.d("GPSTracker", "No Provider is Enabled");

        } else {
            this.canGetLocation = true;
            // First get location from Network Provider

            if (isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                Log.d("GPSTracker", "Network Enabled");

                if (locationManager != null) {

                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (location != null) {

                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        altitude = location.getAltitude();
                        speed = location.getSpeed();
                        bearings = location.getBearing();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {

                if (location == null) {

                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    Log.d("GPSTracker", "GPS Enabled");

                    if (locationManager != null) {

                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if (location != null) {

                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            altitude = location.getAltitude();
                            speed = location.getSpeed();
                            bearings = location.getBearing();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}

Is there any solution to get location based on WiFi/Dataconnection from 4.4 kitkat onwards??

Any help/suggestion would be appreciated.

解决方案

The steps to be followed :

Step 1 : check the following permission in Manifest File

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

Step 2 : check the service class

import android.annotation.SuppressLint;


public class  GPSTracker extends Service implements LocationListener {

public static boolean isGPSEnabled = false;
public static boolean isNetworkEnabled = false;
static boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 2; // 2 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManager;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
@SuppressWarnings("static-access")
@Override
public void onCreate() {
    super.onCreate();
    Log.d("onCreate", "Success !");
        latitudeAndLongtitude();
}

public void latitudeAndLongtitude()
{
    locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (!isGPSEnabled && !isNetworkEnabled) {
        Toast.makeText(getApplicationContext(), "No provider found!", Toast.LENGTH_SHORT).show();
    }else{
        GPSTracker.canGetLocation = true;
        if(isGPSEnabled)
        {
            if (location == null) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            Log.d("Network", "GPS");
            if (locationManager != null) {
                Log.d("locationManager", "is not null ");
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (location != null) {
                    Log.d("Network", "GPS lat and long gets ");
                    Toast.makeText(getApplicationContext(), "isNetworkEnabled!", Toast.LENGTH_SHORT).show();
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    Helper.savePreferences(getApplicationContext(), NameConversion.LATITUDE,String.valueOf(latitude));
                    Helper.savePreferences(getApplicationContext(), NameConversion.LONGITUDE,String.valueOf(longitude));
                }else{
                    Log.d("location", "is getting null ");
                }
            }
        }
    }
        else if(isNetworkEnabled)
        {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            Log.d("Network", "Network");
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (location != null) {
                    //Toast.makeText(getApplicationContext(), "isGPSEnabled!", Toast.LENGTH_SHORT).show();
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    Helper.savePreferences(getApplicationContext(), NameConversion.LATITUDE,String.valueOf(latitude));
                    Helper.savePreferences(getApplicationContext(), NameConversion.LONGITUDE,String.valueOf(longitude));
                }
            }
        }
    }
}
public Location getLocation() {
    return location;
}
/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}
/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }
    // return latitude
    return latitude;
}
/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }
    // return longitude
    return longitude;
}

public void sendLocationToServer() {
    Log.i("sendLocationToServer", "Called!");

}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("onDestroy", "Called!");
    stopSelf();

}

/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
@SuppressWarnings("static-access")
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog On pressing Settings button will
 * lauch Settings Options
 * */
@SuppressLint("NewApi")

@Override
public void onLocationChanged(Location location) {

    latitudeAndLongtitude();
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}}

step 3 : Enable the service in Manifest file

inside application tag

  <service
        android:name=".your package name.GPSTracker"
        android:enabled="true" >
    </service>

setp 4 : start the service in where you want

Hope this will work if you face stil problem may be your mobile will not get the GPS and Network Locaiton .

这篇关于在 Lollipop 5.0.2 中,位置返回 0.0,带有 WIFI/3G/4G的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆