【问题标题】:Unable to find the latitude and longitude找不到经纬度
【发布时间】:2012-08-05 09:48:48
【问题描述】:

我编写了用于查找某个地方的纬度和经度的代码,它在我使用 Eclipse 的 PC 上运行良好,但在我的 Android 手机上却无法运行。

public class MainActivity extends Activity {

    protected LocationManager locationManager;
    protected Button retrieveLocationButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1,
                1000, new MyLocationListener());
        retrieveLocationButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showCurrentLocation();
            }
        });
    }

    protected void showCurrentLocation() {
        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude());
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
                    .show();
        }
    }

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude());
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
                    .show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(MainActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(MainActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(MainActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }
    }

}

【问题讨论】:

  • 你能给我们多一点继续吗?代码等
  • 每当你提到你写了一些代码时,通过发布一些相关的代码让我们知道你写了什么。
  • 如果您正在使用设备,请确保您的设备的 GPS 以及网络位置已打开,因为有时获取 GPS 需要一些时间,直到它只会返回 0.0。
  • @AdeelPervaiz 是的,我打开了 GPS。如何查看网络位置是启用还是禁用?

标签: android gps locationmanager android-location locationlistener


【解决方案1】:

确保您已在 AndroidManifest 文件中授予访问 gps 所需的所有权限...

android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.READ_PHONE_STATE
android.permission.INTERNET

查看此链接:https://sites.google.com/site/androidhowto/how-to-1/using-the-gps

【讨论】:

  • 我认为我的代码和那个代码很相似。它在我的电脑上运行良好,但在我的安卓手机上却不行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多