除非位置已更改,否则无法获取位置坐标

时间:2014-05-04 03:42:18

标签: android gps location locationmanager locationlistener

这是我需要做的。我需要启动我的应用程序,单击按钮,我需要显示当前坐标,即纬度和经度。我按照this教程使用了以下代码:

public class MainActivity extends Activity {

    public double latitude;
    public double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        final TextView latValueLabel = (TextView)findViewById(R.id.latLabel);
        final TextView lonValueLabel = (TextView)findViewById(R.id.lonLabel);
        Button setButton = (Button)findViewById(R.id.set_button);
        setButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub              
                latValueLabel.setText(String.valueOf(latitude));
                lonValueLabel.setText(String.valueOf(longitude));
            }
        });
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if(location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

(复制仅粘贴部分代码,请忽略任何未公开的括号或类似内容。)

随着位置的变化,它会持续获取纬度经度并将其存储到两个double变量latitudelongitude中,当点击setButton时,它会显示最后存储的lat-lon值。这将是用户的当前位置。现在的问题是,我启动了应用程序,但仍然保持应用程序启动的确切位置,我点击了设置按钮。但此时位置没有改变,因此纬度和经度显示为零,这是双变量的默认值。我需要在设备上四处走动,以便在我获得实际坐标之前更改位置。一旦应用程序启动,我怎样才能获得lat-lon?

2 个答案:

答案 0 :(得分:2)

您可以使用getLastKnownLocation(...)初始化经度和纬度值,如下所示:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

double longitude = location.getLongitude();
double latitude = location.getLatitude();

答案 1 :(得分:-2)

这是你的全班。

public class MainActivity extends Activity {

    public double latitude;
    public double longitude;
    private TextView latValueLabel,lonValueLabel ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        latValueLabel = (TextView)findViewById(R.id.latLabel);
        lonValueLabel = (TextView)findViewById(R.id.lonLabel);
        Button setButton = (Button)findViewById(R.id.set_button);
        setButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {             
                latValueLabel.setText(String.valueOf(latitude));
                lonValueLabel.setText(String.valueOf(longitude));
            }
        });
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            if(location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                latValueLabel.setText(String.valueOf(latitude));
                lonValueLabel.setText(String.valueOf(longitude));
            }
        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }
    }
}