【问题标题】:Not getting location coordinates unless location has changed除非位置已更改,否则无法获取位置坐标
【发布时间】:2014-05-04 03:42:18
【问题描述】:

这是我需要做的。我需要启动我的应用程序并单击一个按钮,我需要显示当前坐标,即纬度和经度。我遵循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时,它会显示最后存储的纬度值。那将是用户的当前位置。现在的问题是,我启动了应用程序,但仍然停留在启动应用程序的确切位置,我单击了设置按钮。但是当时位置并没有改变,所以纬度和经度显示为零,这是双变量的默认值。我需要带着设备四处走走,以便在获得实际坐标之前更改位置。如何在应用启动后立即获得经纬度?

【问题讨论】:

  • 使用locationManager.getLastKnownLocation(... )初始化经纬度值。

标签: android gps location locationmanager locationlistener


【解决方案1】:

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

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

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

【讨论】:

  • 如果我用这个,我就完全不用LocationListener了吧?
  • 是的,但这只是返回最后一个已知位置。要获取位置更新,您仍然需要使用LocationListenergetLastKnownLocation() 顾名思义就是返回最后一个已知位置。如果用户禁用 GPS 一段时间,最后一个已知位置可能完全错误,但您对此无能为力,因此我不会担心。
  • 好吧,当我尝试仅使用getLastKnownlocation() 使用设备移动时,它从未更新当前位置,无论我走多远,我仍然打开 GPS,而位置监听器已更新它正确。所以getLastKnownLocation() 从不要求新的位置?
  • 您应该只使用getLastKnownLocation() 在您的应用程序开始时初始化经度和纬度值。当新的位置数据可用时,getLastKnownLocation() 返回的值不会立即更新。据我所知,在对位置进行适当修复后,值会缓慢更新。只要您的应用正在运行,请依靠 LocationListener 获取位置数据。
  • getLastKnownLocation() 是非阻塞的。即使它被阻止我也不介意,因为获得准确的位置对我的应用程序非常重要。无论如何,只要按下按钮,我就可以不惜一切代价获得确切的位置吗?
【解决方案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) {

        }
    }
}

【讨论】:

  • 这不回答问题...阅读问题一次。这并不比您的其他答案更正确。请不要发布多个答案。改为编辑您的旧答案。
猜你喜欢
  • 1970-01-01
  • 2015-04-14
  • 2018-08-11
  • 1970-01-01
  • 2012-05-27
  • 2016-08-16
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多