让我给你一个简短的背景。
我需要在一段时间或一段距离后检查用户的位置。所以我使用 LocationListner 类来跟踪位置。为了在模拟器上测试它,我创建了 .gpx 文件来设置方向。我正在通过按钮单击获得模拟器上的位置。但是我如何能够根据距离或时间获取更新的位置。 如developer.adnroid所述
You can control the frequency at which your listener receives updates with the second
and third parameter—the second is the minimum time interval between notifications and
the third is the minimum change in distance between notifications—setting both to
zero requests location notifications as frequently as possible.
但我没有得到预期的结果。有人知道我在做什么错吗??
代码片段
在按钮上单击这段代码执行
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener locListener = new MyLocationListener(this, locManager);
boolean isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled)
{
Toast.makeText(getApplicationContext(), "Called isGPSEnabled", Toast.LENGTH_SHORT).show();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, locListener);
}
仅供参考:如果被调用,则在里面吐司。
这是我的 onLoactionChange。仅在其余方法为空时才添加此方法。
@Override
public void onLocationChanged(Location location)
{
if(location != null)
{
locManager.removeUpdates(this);
Geocoder gcd = new Geocoder(context.getApplicationContext(), Locale.getDefault());
List<Address> addresses;
try
{
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0)
{
String cityName = addresses.get(0).getAddressLine(0) + "\n";
cityName = cityName + addresses.get(0).getAddressLine(1);
TextView tvInfo = (TextView) ((Activity) context).findViewById(R.id.tv_info);
tvInfo.setVisibility(View.VISIBLE);
tvInfo.setText("Curent address is" + cityName + "Latitude :" + addresses.get(0).getLatitude() + "Longitude :" +addresses.get(0).getLongitude());
}
} catch (IOException e)
{
//Toast
Toast.makeText(context.getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
清单具有权限为
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
结果: 单击按钮后,它会给出当前位置。但始终如此。
预期结果:经过一段时间或移动一定距离后,当前位置应该更新。
任何预兆都可以帮助我实现这一目标。或指出我在哪里做错了。??