将 lastKnowLocation 转换为 String 或 CharSeqance,然后使用 Toast.maketext 打印出来,但失败

发布于 2024-12-12 03:16:15 字数 1165 浏览 0 评论 0原文

我从 Android 获得了 GPS 坐标,并且想使用 toast.maketext 方法打印出坐标。我的代码如下。

    LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener LocListener = new LocationListener(){

        public void onLocationChanged(Location loc) {

        }

        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) {

        }

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

        }
    };

    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, LocListener);

    Location lastKnownLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double latitude=lastKnownLocation.getLatitude();
    double longtitude=lastKnownLocation.getLongitude();
    CharSequence coordinate="Latitude = " + Double.toString(latitude) + "\nLongitude = " + Double.toString(longtitude);

    Toast.makeText(getApplicationContext(), coordinate, Toast.LENGTH_SHORT).show();

无论坐标是字符串还是字符序列,应用程序都无法运行,因为误用了Toast.maketext。但 Eclipse 编译器从未识别出这个问题。我该如何处理这个问题?

I have got GPS coordinates from Android, and I want to print out coordinates using the toast.maketext method. My code is like the following.

    LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener LocListener = new LocationListener(){

        public void onLocationChanged(Location loc) {

        }

        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) {

        }

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

        }
    };

    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, LocListener);

    Location lastKnownLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double latitude=lastKnownLocation.getLatitude();
    double longtitude=lastKnownLocation.getLongitude();
    CharSequence coordinate="Latitude = " + Double.toString(latitude) + "\nLongitude = " + Double.toString(longtitude);

    Toast.makeText(getApplicationContext(), coordinate, Toast.LENGTH_SHORT).show();

No matter coordinate is string or charSequence, the application cannot run because of misuse of Toast.maketext. But the Eclipse compiler never identifies this problem. How would I handle this problem?

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

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

发布评论

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

评论(1

我喜欢麦丽素 2024-12-19 03:16:15

你做错了。 getLastKnownLocation() 仅给出最后一个缓存位置。例如,如果手机关闭,则可能没有这样的缓存位置。

onLocationChanged() 回调中,您检查 Location 对象是否为 null,如果不是,则从 Location 获取(纬度,经度) >Location 对象从那里发布一个 Toast。

另外,在这种情况下,最好使用 LogCat 进行调试,您每秒都会获得位置修复。在 onLocationChanged() 回调中使用 Log.d("WOOO", "Location returned" + loc.getLatitude() + " " + loc.getLongitude());

You're doing it wrong. getLastKnownLocation() only gives the last cached location. There may be no such cached location if the phone was off, for example.

In the onLocationChanged() callback you check if the Location object is null, if not, get the (latitude,longitude) from the Location object post a Toast from there.

Also, in this case it is better to use LogCat for debugging, you will be getting location fixes every second. Use Log.d("WOOO", "Location received" + loc.getLatitude() + " " + loc.getLongitude()); in the onLocationChanged() callback.

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