将 lastKnowLocation 转换为 String 或 CharSeqance,然后使用 Toast.maketext 打印出来,但失败
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你做错了。
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 theLocation
object isnull
, if not, get the (latitude,longitude) from theLocation
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. UseLog.d("WOOO", "Location received" + loc.getLatitude() + " " + loc.getLongitude());
in theonLocationChanged()
callback.