【问题标题】:getting current location longitude and latitude in android在android中获取当前位置的经度和纬度
【发布时间】:2012-07-08 12:25:33
【问题描述】:

这是我的位置类,我正在获取位置经度和纬度,但我的问题是如果我更改当前位置,经度和纬度值不会更新。

如果我将我的家庭位置存储在我的设备中,那么我的应用程序每次都会采用相同的经度和纬度值,而不会采用我当前的位置详细信息。例如:我今天在纽约,我启动了一个应用程序,位置是我在新泽西州的家。

public class GetLocation extends Activity {
    protected LocationManager locationManager;
    protected Button retrieveLocationButton;
    Context mContext;
    Double Longitude, Latitude;
    String NetworkErrmsg = "", GpsErrmsg = "", ProviderErrmsg = "", NoNetworkmsg = "", GpsSuccesmsg = "", NetworkSuccesmsg = "";
    public GetLocation(Context mContext) {
        // TODO Auto-generated constructor stub
        this.mContext = mContext;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
   public void getLocationDetails() {
      locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 100, new MyLocationListener());
        boolean gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!gps_enabled) {
            GpsErrmsg = "No Gps Connectivity";
        } else {
            showCurrentLocation(LocationManager.GPS_PROVIDER);
            GpsSuccesmsg = "Gps Enabled";
        }
        if (network_enabled && !gps_enabled) {
            showCurrentLocation(LocationManager.NETWORK_PROVIDER);
            NetworkSuccesmsg = "Network Provider Enabled. Please Enable your GPS settings to get exact onaround you list";
        }
        if (!gps_enabled && !network_enabled) {
            NoNetworkmsg = "Please Enable your Network/GPS settings to get onaround you list";
        }
    }
    protected void showCurrentLocation(String provider) {
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            Longitude = location.getLongitude
            Latitude = location.getLatitude();
        } else {
            NoNetworkmsg = "Please Enable your Network/GPS settings to get onaround you list";
            Longitude=null; Latitude=null;
        }
    }
    private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) { 
            System.out.println("Longitude " + location.getLongitude());
        }
        @Override
        public void onStatusChanged(String s, int i, Bundle b) {
            // Toast.makeText(GetLocation.this, "Provider status changed", Toast.LENGTH_LONG).show();
        }
        @Override
        public void onProviderDisabled(String s) {
            // Toast.makeText(GetLocation.this, "Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
        }
        @Override
        public void onProviderEnabled(String s) {
            // Toast.makeText(GetLocation.this, "Provider enabled by the user. GPS turned on", Toast.LENGTH_LONG).show();
        }
    }
}

【问题讨论】:

    标签: android google-maps latitude-longitude locationmanager


    【解决方案1】:
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.util.Log;
    import android.os.Bundle;
    import android.widget.Toast;
    
    
    
    
    public class GPSLocationBased extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locationbased);
    
    
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new Mylocationlistener();
    
    
    
    
    
        // ---Get the status of GPS---
        boolean isGPS = lm
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
        // If GPS is not enable then it will be on
        if(!isGPS)
        {
            Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
            intent.putExtra("enabled", true);
             sendBroadcast(intent);
    
    
        }
    
        //<--registers the current activity to be notified periodically by the named provider. Periodically,
        //the supplied LocationListener will be called with the current Location or with status updates.-->
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }
    
    /**
     *Mylocationlistener class will give the current GPS location 
     *with the help of Location Listener interface 
     */
    private class Mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                // ---Get current location latitude, longitude, altitude & speed ---
    
                Log.d("LOCATION CHANGED", location.getLatitude() + "");
                Log.d("LOCATION CHANGED", location.getLongitude() + "");
                float speed = location.getSpeed();
                double altitude = location.getAltitude();
                Toast.makeText(GPSLocationBased.this,"Latitude = "+
                        location.getLatitude() + "" +"Longitude = "+ location.getLongitude()+"Altitude = "+altitude+"Speed = "+speed,
                        Toast.LENGTH_LONG).show();
            }
        }
    
        @Override
        public void onProviderDisabled(String provider) {
        }
    
        @Override
        public void onProviderEnabled(String provider) {
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
    

    }

    【讨论】:

    • 我认为在我的位置类中我没有在 onLocationChanged 方法中检查位置..这是我的错误吗?如果我在 onlocaitonchanged 方法中调用 getLocationDetails() 方法,它对我有用吗?
    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2011-09-29
    相关资源
    最近更新 更多