【问题标题】:How to get location using GPS?如何使用 GPS 获取位置?
【发布时间】:2014-11-16 09:05:55
【问题描述】:

您好,我正在使用以下代码通过 GPS 获取位置 我已在 AndroidManifest.xml 中授予所有必需的权限

if (DeviceUtilities.isNetworkAvailable(context))
        {
            Toast.makeText(HomeScreen.this, "Getting location from GPS",Toast.LENGTH_SHORT).show();
            LocationManager lm;
            lm = (LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria c = new Criteria();
            c.setAccuracy(Criteria.ACCURACY_FINE);
            String bestProvider = lm.getBestProvider(c, true);
            Location location = lm.getLastKnownLocation(bestProvider);

            if(location!=null)
            {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }

但是得到纬度和经度的空值。 如何获取位置? 请建议。 提前致谢。

【问题讨论】:

    标签: android gps location


    【解决方案1】:

    1) 创建位置监听类TrackLocation

        public class TrackLocation extends Service implements LocationListener {
    
                private final Context mContext;
    
                // we can get location from gps or network
    
                // 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
    
                // distance to change Updates in meters
                private static final long MIN_DISTANCE = 10; // 10 meters
    
                // The minimum time between updates in milliseconds
                private static final long MIN_TIME = 1000 * 60 * 1; // 1 minute
    
                // Declaring a Location Manager
                protected LocationManager locationManager;
    
                public TrackLocation(Context context) {
                    this.mContext = context;
                    getLocation();
                }
    
                public Location getLocation() {
                    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;
                            if (isNetworkEnabled) {
                                locationManager.requestLocationUpdates(
                                        LocationManager.NETWORK_PROVIDER,
                                        MIN_TIME,
                                        MIN_DISTANCE, 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 latitude/longitude using GPS Services
                            if (isGPSEnabled) {
                                if (location == null) {
                                    locationManager.requestLocationUpdates(
                                            LocationManager.GPS_PROVIDER,
                                            MIN_TIME,
                                            MIN_DISTANCE, 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/Wi-Fi 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;
                }
        }  
    

    2) 然后在主类中执行此操作

    TrackLocation gps = new TrackLocation(this);  
    //Check if GPS enabled
    if(gps.canGetLocation()) {
        // now get get coordinates
        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();
    
    } else {
        // Can't get location.
    }
    

    【讨论】:

      【解决方案2】:

      “getLastKnownLocation”如果手机没有最后的位置,则返回null,表示最近其他应用没有请求或获取位置。

      如果您使用 getLastKnowLocation 得到 null,则必须通过询问 gps 或网络来获取位置,并在侦听器中等待响应。

      一些sn-ps代码:

      LocationManager locationManager = (LocationManager)
      getSystemService(Context.LOCATION_SERVICE);
      
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER OR LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
      

      你必须实现你的 locationListener 来接收位置。

      更多信息在这里: http://developer.android.com/reference/android/location/LocationManager.html

      注意:这种方式是使用LocationManager,你也可以使用播放服务,但我更喜欢使用Location Manager。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多