【问题标题】:Android location is always null with both NETWORK_PROVIDER and GPS_PROVIDERNETWORK_PROVIDER 和 GPS_PROVIDER 的 Android 位置始终为空
【发布时间】:2015-07-20 11:25:48
【问题描述】:

这是我尝试从 MainActivity.java 使用的服务类。在模拟器上运行。

package com.example.sonia.project;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener {
    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

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

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker() {
    }

    public Location getLocation(Context mContext) {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(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
            } 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("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // 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("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } 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) {
        Log.d("Location", "Location changed!");
        this.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;
    }

}

这是我的 MainActivity 的 onStart() 方法

@Override
protected void onStart() {
    super.onStart();
    gps = new GPSTracker();
    gps.getLocation(this);
}

然后我在 MainActity 中单击按钮时再次调用 getLocation()

gps.getLocation(this);

关于此问题的公认答案建议获取位置更新并使用多个提供商 - My current location always returns null. How can I fix this?。我都在做。

getLocation() 总是返回一个空位置,即使我在几分钟后尝试获取该位置。怎么做才能让它返回一个有效的位置而不是 null?

【问题讨论】:

  • 你在模拟器还是真机上测试?
  • 模拟器 - Nexus 5 API 22 x86
  • 如果您没有得到答案,请使用 fusedLocation "javapapers.com/android/android-location-fused-provider" 这是示例

标签: android geolocation


【解决方案1】:

要在模拟器中获取位置,您必须通过模拟器控制进行配置

更多信息请参考以下链接

http://wptrafficanalyzer.in/blog/android-gps-with-locationmanager-to-get-current-location-example/

【讨论】:

  • 在我的模拟器中,设置 > 位置显示它已打开,设置 > 位置 > 位置模式显示它正在使用“高精度”模式(使用 GPS、Wi-Fi 和蜂窝网络来确定位置)。但是,我无法在 Android Studio 中找到 Eclipse > DDMS (Perspective) 设置。从您的链接来看,无论如何,它看起来像一个黑客,模拟器没有检测到位置,而是读取您提供给它的硬编码值。
猜你喜欢
  • 2016-06-15
  • 2017-11-08
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
相关资源
最近更新 更多