【问题标题】:Location returns 0.0 with WIFI/3G/4G in Lollipop 5.0.2在 Lollipop 5.0.2 中,位置返回 0.0,带有 WIFI/3G/4G
【发布时间】:2015-03-05 19:10:09
【问题描述】:

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

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

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;
}

}

是否有任何解决方案可以根据WiFi/Dataconnection 从4.4 kitkat 开始获取位置??

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

【问题讨论】:

  • 请贴出相关代码
  • @DavidWasser 代码添加
  • 请尝试更有效的获取位置的方法,例如 Fused Location API - mobilewebwizard.in/2015/03/fused-location-api-android-example
  • @Vipinhelloindia 它也不起作用。检查Android hive 示例,它使用相同的api
  • 我会试试的,如果是,你可以向谷歌报告错误。

标签: android gps location android-5.0-lollipop


【解决方案1】:

要遵循的步骤:

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

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

第二步:检查服务类

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) {
}}

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

在应用程序标签内

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

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

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

【讨论】:

  • 您首先检查网络是否启用。然后再次启用检查 GPS。如果它需要网络价值,你就没有得到正确的纬度和经度。你的代码将检查这两个选项我的不会同时采用它的第一个首选 GPS
  • 你的服务没有onCreate方法???当你启动服务时,它会从哪里执行???
  • 我从来没有遇到过服务问题。每当我检查它启用网络时,它总是返回错误。我从 android hive 测试了演示,结果相同。那么网络从 4.4 开始就对你有用了吗?
  • 是的,我也得到了 GPS 值和网络值。换个sim卡提供商,关掉手机再开机试试。确保 GPS 已开启。并检查 LOG cat 的 lat 和 long 值
  • 如果 GPS 开启,则返回纬度和经度。没有 GPS 它不会返回任何东西。将尝试使用不同的 SIM 卡。
【解决方案2】:

当您致电locationManager.requestLocationUpdates() 时,这会告诉 Android 您希望在用户位置更改时被回叫。如果您没有启用 GPS 并且想要使用 NETWORK_PROVIDER,则需要有 Internet 访问权限。这一切都是异步发生的。你打电话给requestLocationUpdates(),然后一段时间后,Android 会用新的位置调用onLocationChanged() 给你回电。

您在调用requestLocationUpdates() 后所做的就是立即调用getLastKnownLocation(),这可能会或可能不会返回有用的东西。在您实施onLocationChanged() 时,您什么也没做。您需要等到 Android 调用 onLocationChanged(),此时您应该将传递的 Location 参数存储在您的成员变量中。

【讨论】:

  • isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER);4.4 开始总是返回false
  • 你试过重启手机了吗?设置中是否启用了网络位置?这听起来有点奇怪。
  • 你在什么设备上测试?
  • MOTO G & LG G2 设备
  • 对这个问题有任何想法吗?
【解决方案3】:

我认为使用FusedLocationApi 会是更好的解决方案,请参阅https://developer.android.com/training/location/retrieve-current.html

【讨论】:

  • 根据android hive 示例,它使用FusedLocationApi 并且它也不起作用
猜你喜欢
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多