【发布时间】:2012-02-02 14:51:07
【问题描述】:
我有一个带有按钮的 Activity,我只希望该按钮通过 gps/internet/etc 获取当前用户位置并显示带有这些坐标的 toast。就是这样,只需检查一次坐标,然后显示出来。只有每次单击按钮时,坐标才会更新并显示。
我该怎么做?我在理解 LocationManager 时遇到了一些麻烦(如果那是我应该使用的)
我在清单中有这些
"android.permission.INTERNET"
"android.permission.ACCESS_FINE_LOCATION"
"android.permission.ACCESS_NETWORK_STATE"
"android.permission.ACCESS_COARSE_LOCATION"
logcat 仅在应用启动时显示无法获取连接工厂客户端。该应用程序首先显示一个地图视图,并且工作正常,所以我不知道该错误是否是我的问题。
我做到了:
...
LocationManager mlocManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
....
public class MyLocationListener implements LocationListener
{ @Override public void onLocationChanged(Location loc)
{
TextView textView = (TextView) findViewById(R.id.textView2);
textView.setText("Latitud: "+loc.getLatitude()+" , Longitud"+loc.getLongitude());
}
...
}
但是它没有改变 textview2 值
【问题讨论】:
-
StackOverflow 用于编程问题。这不是一个问题。
-
编辑了标题。对不起*
-
位置被更新,onLocationChanged 方法在 GPS 芯片得到修复并且操作系统决定提供它时运行,而不是在您按下按钮时。
-
@NickT 这就是答案,我不能,谢谢。
-
看看这个很棒的教程,它可以满足您的需求。你也可以下载源代码。 androidhive.info/2012/07/android-gps-location-manager-tutorial
标签: android google-maps geolocation gps