简体   繁体   English

Android 4.4.2-获取设备位置坐标

[英]Android 4.4.2 - Get Device Location Coordinates

I'm using android 4.4.2 and I'm trying to get the device location coordinates without success. 我正在使用android 4.4.2,并且试图获取设备位置坐标而没有成功。

This is the code I'm using : 这是我正在使用的代码:

On Manifest: 在清单上:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

On class: 上课:

   private double[] getLastBestLocation() {
            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
            List<String> providers = lm.getProviders(true);

            /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
            Location l = null;

            for (int i=providers.size()-1; i>=0; i--) {
                    l = lm.getLastKnownLocation(providers.get(i));
                    if (l != null) break;
            }

            double[] gps = new double[2];
            if (l != null) {
                    gps[0] = l.getLatitude();
                    gps[1] = l.getLongitude();
            }
            return gps;
    }

I've tried different answers provided at SO, specifically: 我尝试了SO提供的不同答案,特别是:

How do I get the current GPS location programmatically in Android? 如何在Android中以编程方式获取当前GPS位置?
How to get Android GPS location 如何获取Android GPS位置

Please don't mark this question as duplicated since other answers didn't solve my problem. 请不要将此问题标记为重复,因为其他答案不能解决我的问题。

This code uses both GPS as well as network for finding location. 此代码同时使用GPS和网络来查找位置。

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

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

if (isGPSEnabled) {
    if (location == null) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this, null);
        if (locationManager != null) {
             location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();

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

if (isNetworkEnabled) {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this, null);
    Log.v("Network", "Network is enabled");
    if (locationManager != null) {
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {                         
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            Log.v("LocationTracker", "Location : "+latitude+", "+longitude);
        }
        else
        {
                Log.v("LocationTracker", "Location is null");
        }
    }
    else
    {
        Log.v("LocationTracker","Location manager is null");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM