三、自动跟踪
当手机在移动时,GPS坐标也在变化,为了可以看到自己实时位置,我们需要添加一个动态更新的功能。大家可以想到,这又是一个listener。
在oncreate中,新建一个位置管理对象:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
该函数将GPS服务注册至this, 即listener。
在程序实体上添加:
public class AndroidLBS extends MapActivity implements LocationListener
为了实现该implementation,我们要加入下面方法:

代码中的onLocationChanged方法的监测周期为1秒,在500米距离的改变之后进行调用。默认使用的 LocationProvider是“gps” (GPS_PROVIDER) 。可以根据需要,使用其他的 LocationProvider,如定位更加准确的卫星或者网络数据。