嘿,我目前正在研究如何将AndEngine与手机的 GPS 位置一起使用。我认为访问这些信息应该不会太难,所以我只是抓住了ChangeableText示例并尝试对其进行调整,以便我可以看到它正确地抓住了位置,基本上我所做的是:
在类声明中添加了“实现 LocationListener”
public class ChangeableTextExample extends BaseExample implements LocationListener{
向类字段添加了位置变量和位置管理器:
private long curlat;
private long curlng;
private LocationManager locationManager;
在 onLoadScene() 方法中实例化 locationManager:
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
设置 elapsedText 以打印出 curlat 和 curlng 变量:
scene.registerUpdateHandler(new TimerHandler(1 / 20.0f, true, new ITimerCallback() {
public void onTimePassed(final TimerHandler pTimerHandler) {
elapsedText.setText("Location: " + curlat + ", " + curlng);
fpsText.setText("FPS: " + fpsCounter.getFPS());
}
}));
并实现 onLocationChanged(Location arg) 方法,以便更新 curlat、curlng 字段。
public void onLocationChanged(Location location) {
this.curlat = (long) (location.getLatitude()*1E6);
this.curlng = (long) (location.getLongitude()*1E6);
}
可悲的是,这不起作用,我想知道你们中是否有人能指出我正确的方向?我对 Android 编程很陌生,甚至对使用 AndEngine 编程也很陌生。我猜这与 locationManager 没有正确实例化有关吗?
谢谢!