嘿,我目前正在研究如何将AndEngine与手机的GPS位置一起使用。我认为它不应该太难以访问这些信息,所以我只是抓住了ChangeableText的例子,并试图调整它,所以我可以看到它正确地抓住了位置,基本上我所做的是:在AndEngine游戏引擎旁边使用LocationListener
添加'实现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(位置ARG)方法,它更新curlat,curlng领域。
public void onLocationChanged(Location location) {
this.curlat = (long) (location.getLatitude()*1E6);
this.curlng = (long) (location.getLongitude()*1E6);
}
可悲的是,这是不行的,我想知道如果你们能在正确的方向指向我?我对Android编程相当陌生,甚至更喜欢使用AndEngine进行编程。我猜这与locationManager没有正确实例化的事实有关。
谢谢!
你在一个设备或模拟器上测试吗? – 2011-03-11 17:34:11
设备。在完成一些代码后,我每隔五分钟就会到外面走走并测试GPS。这是一个痛苦。 – Simon 2011-03-11 17:37:06