地点返回0.0与WIFI / 3G / 4G的棒棒堂5.0.2 [英] Location returns 0.0 with WIFI/3G/4G in Lollipop 5.0.2

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

问题描述

我想根据 WIFI / 3G / 4G 连接获取的位置,但它总是返回 0.0 经纬度。如果同一code为使用 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;
布尔canGetLocation = FALSE;

地点位置; // 位置
双纬度; // 纬度
双经度; // 经度
双速;
双高度;
双轴承;

//最小距离改变以米为单位更新
私有静态最后长MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; //10米

//毫秒更新之间的最短时间
私有静态最后长MIN_TIME_BW_UPDATES = 5000; //10秒

保护LocationManager locationManager;

公共GPSTracker(上下文的背景下){
    this.mContext =背景;
    的getLocation();
}

公共场所的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);

        如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled){
            //没有网络提供商启用

            Log.d(GPSTracker,无商已启用);

        } 其他 {
            this.canGetLocation = TRUE;
            //首先从网络提供商处获取位置

            如果(isNetworkEnabled){

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);

                Log.d(GPSTracker,网络启用);

                如果(locationManager!= NULL){

                    位置= locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    如果(位置!= NULL){

                        纬度= location.getLatitude();
                        经度= location.getLongitude();
                        高度= location.getAltitude();
                        速度= location.getSpeed​​();
                        轴承= location.getBearing();
                    }
                }
            }
            //如果GPS功能的获得纬度/经度使用GPS服务
            如果(isGPSEnabled){

                如果(位置== NULL){

                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);

                    Log.d(GPSTracker,GPS功能的);

                    如果(locationManager!= NULL){

                        位置= locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        如果(位置!= NULL){

                            纬度= location.getLatitude();
                            经度= location.getLongitude();
                            高度= location.getAltitude();
                            速度= location.getSpeed​​();
                            轴承= location.getBearing();
                        }
                    }
                }
            }
        }

    }赶上(例外五){
        e.printStackTrace();
    }

    返回的位置;
}

/ **
 *停止使用GPS监听器调用此函数将停止使用GPS在你的
 *应用程序
 * * /
公共无效stopUsingGPS(){
    如果(locationManager!= NULL){
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/ **
 *功能得到纬度
 * * /
公共双getLatitude(){
    如果(位置!= NULL){
        纬度= location.getLatitude();
    }

    //返回纬度
    返回纬度;
}

/ **
 *功能得到经度
 * * /
公共双getLongitude(){
    如果(位置!= NULL){
        经度= location.getLongitude();
    }

    //返回经度
    返回经度;
}

/ **
 *功能检查GPS / WiFi功能
 *
 * @返回布尔
 * * /
公共布尔canGetLocation(){
    返回this.canGetLocation;
}

@覆盖
公共无效onLocationChanged(位置定位){
}

@覆盖
公共无效onProviderDisabled(字符串提供商){
}

@覆盖
公共无效onProviderEnabled(字符串提供商){
}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}

@覆盖
公众的IBinder onBind(意向为arg0){
    返回null;
}

}
 

有没有什么解决方案基于无线网络/数据 4.4奇巧起??

任何帮助/建议,将AP preciated。

解决方案
  应遵循

的步骤:

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

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_MOCK_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
 

  

第2步:检查服务类

 进口android.annotation.Sup pressLint;


公共类GPSTracker扩展服务实现LocationListener的{

公共静态布尔isGPSEnabled = FALSE;
公共静态布尔isNetworkEnabled = FALSE;
静态布尔canGetLocation = FALSE;

地点位置; // 位置
双纬度; // 纬度
双经度; // 经度
私有静态最后长MIN_DISTANCE_CHANGE_FOR_UPDATES = 2; //2米
私有静态最后长MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟
保护LocationManager locationManager;

@覆盖
公众的IBinder onBind(意向为arg0){
    返回null;
}
@燮pressWarnings(静态访问)
@覆盖
公共无效的onCreate(){
    super.onCreate();
    Log.d(的onCreate,成功!);
        latitudeAndLongtitude();
}

公共无效latitudeAndLongtitude()
{
    locationManager =(LocationManager)getApplicationContext()getSystemService(LOCATION_SERVICE)。
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled){
        Toast.makeText(getApplicationContext(),无商找到了!,Toast.LENGTH_SHORT).show();
    }其他{
        GPSTracker.canGetLocation = TRUE;
        如果(isGPSEnabled)
        {
            如果(位置== NULL){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,本);
            Log.d(网络,GPS);
            如果(locationManager!= NULL){
                Log.d(locationManager,不为空);
                位置= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                如果(位置!= NULL){
                    Log.d(网络,GPS纬度和长变);
                    Toast.makeText(getApplicationContext(),isNetworkEnabled!,Toast.LENGTH_SHORT).show();
                    纬度= location.getLatitude();
                    经度= location.getLongitude();
                    Helper.save preferences(getApplicationContext(),NameConversion.LATITUDE,将String.valueOf(纬度));
                    Helper.save preferences(getApplicationContext(),NameConversion.LONGITUDE,将String.valueOf(经度));
                }其他{
                    Log.d(位置,渐空);
                }
            }
        }
    }
        否则,如果(isNetworkEnabled)
        {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,本);
            Log.d(网络,网络);
            如果(locationManager!= NULL){
                位置= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                如果(位置!= NULL){
                    //Toast.makeText(getApplicationContext(),isGPSEnabled!,Toast.LENGTH_SHORT).show();
                    纬度= location.getLatitude();
                    经度= location.getLongitude();
                    Helper.save preferences(getApplicationContext(),NameConversion.LATITUDE,将String.valueOf(纬度));
                    Helper.save preferences(getApplicationContext(),NameConversion.LONGITUDE,将String.valueOf(经度));
                }
            }
        }
    }
}
公共场所的getLocation(){
    返回的位置;
}
/ **
 *停止使用GPS监听器调用此函数将停止使用GPS在你的
 *应用程序
 * * /
公共无效stopUsingGPS(){
    如果(locationManager!= NULL){
        locationManager.removeUpdates(GPSTracker.this);
    }
}
/ **
 *功能得到纬度
 * * /
公共双getLatitude(){
    如果(位置!= NULL){
        纬度= location.getLatitude();
    }
    //返回纬度
    返回纬度;
}
/ **
 *功能得到经度
 * * /
公共双getLongitude(){
    如果(位置!= NULL){
        经度= location.getLongitude();
    }
    //返回经度
    返回经度;
}

公共无效sendLocationToServer(){
    Log.i(sendLocationToServer,叫!);

}

@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    Log.i(的onDestroy,叫!);
    stopSelf();

}

/ **
 *功能检查GPS / WiFi功能
 *
 * @返回布尔
 * * /
@燮pressWarnings(静态访问)
公共布尔canGetLocation(){
    返回this.canGetLocation;
}

/ **
 *功能,以显示设置在pressing设置按钮警告对话框
 *劳克设置选项
 * * /
@燮pressLint(NewApi)

@覆盖
公共无效onLocationChanged(位置定位){

    latitudeAndLongtitude();
}

@覆盖
公共无效onProviderDisabled(字符串提供商){
}

@覆盖
公共无效onProviderEnabled(字符串提供商){
}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}}
 

  

第3步:打开清单文件服务

     

里面的应用程序标记

 <服务
        机器人:NAME =你的包name.GPSTracker。
        机器人:启用=真正的>
    < /服务>
 

  

值存取4:开始在您需要的服务

     

希望,如果你面对还是老样子问题可能是您的手机将无法获得GPS和网络Locaiton这将工作。

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 .

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

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