如何在 Android (2.2) 设备上获取用户的 GPS 位置

发布于 2024-10-17 04:50:53 字数 1419 浏览 3 评论 0原文

我试图简单地获取用户的 GPS 位置,但在尝试获取设备的纬度和经度时我的应用程序崩溃了。请参阅代码以了解错误发生的位置。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    final double longitude = location.getLongitude();  //error occurs here
    final double latitude = location.getLatitude();    //and here

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
            //longitude = location.getLongitude();
            //latitude = location.getLatitude();
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
      };
    //update location via GPS PROVIDER
    //can also use LocationManager.NETWORK_PROVIDER
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

}

我还在清单文件中包含了 ACCESS_FINE_LOCATION 权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

所以我不太确定为什么这不起作用。任何帮助将不胜感激!

I am attempting to simply acquire the user's GPS location, but my application crashes when trying to get the latitude and longitude of the device. See code for where the error occurs..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    final double longitude = location.getLongitude();  //error occurs here
    final double latitude = location.getLatitude();    //and here

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
            //longitude = location.getLongitude();
            //latitude = location.getLatitude();
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
      };
    //update location via GPS PROVIDER
    //can also use LocationManager.NETWORK_PROVIDER
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

}

I am also including the ACCESS_FINE_LOCATION permission in the manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

so I'm not quite sure why this does not work. Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2024-10-24 04:50:53

您的错误很可能位于: lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

这是因为 getLastKnownLocation 是非阻塞调用,并且假设应用程序刚刚启动,系统没有“最后已知位置”。您可能想要循环,直到 getLastKnownLocation 没有获得非空值为止。

Your error is most probably at: lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

It is because the getLastKnownLocation is a non blocking call and assuming the application has just started there is no 'last known location' that the system has. You might want to loop till you dont get a non-null value for getLastKnownLocation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文