Android 如何在单击按钮时检索位置
我希望能够做到这一点: 我有一个Android 如何在单击按钮时检索位置,android,locationmanager,android-location,locationlistener,Android,Locationmanager,Android Location,Locationlistener,我希望能够做到这一点: 我有一个文本视图和一个按钮。当我点击按钮时,文本视图的文本应设置为我当前所在地的名称 我找到了一些使用LocationManager和LocationListener的示例,但似乎LocationListener侦听onLocationChanged 关于如何实现我想做的事情有什么想法吗?请使用谷歌地图api获取当前位置,然后从给定的lat长度打印该位置的地址。我知道它的答案并不详细,但我确信它将为您提供开始解决问题的提示。试试这个,这是最好的方法 LocationMan
文本视图
和一个按钮
。当我点击按钮
时,文本视图
的文本应设置为我当前所在地的名称
我找到了一些使用
LocationManager
和LocationListener
的示例,但似乎LocationListener
侦听onLocationChanged
关于如何实现我想做的事情有什么想法吗?请使用谷歌地图api获取当前位置,然后从给定的lat长度打印该位置的地址。我知道它的答案并不详细,但我确信它将为您提供开始解决问题的提示。试试这个,这是最好的方法 LocationManager mLocManager=(LocationManager)getSystemService(Context.LOCATION\u服务); LocationListener mLocListener=新建MyLocationListener(); mLocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,mLocListener) 公共类MyLocationListener实现LocationListener{
public void onLocationChanged(Location loc) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
您是否已在设备中检查GPS位置是否已打开NLOCATIONCHANGED将起作用,因为它将获取您可以存储为变量的第一个位置。如果您移动它,它将更新,如果不移动,则无需更新,因为您已经存储了位置。
onLocationChanged
似乎没有执行,因此我没有收到任何更改位置更新。我也像你一样尝试过。我也尝试过这个解决方案,你的解决方案几乎就是这样。同样的问题:onLocationChanged
似乎从未执行过。我在做什么?