0

嘿,我目前正在研究如何将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 没有正确实例化有关吗?

谢谢!

4

2 回答 2

1

请参阅Android 获取用户位置

您必须实际告诉 locationManager 对象为您提供更新(以及在 AndroidManifest.xml 中放置位置信息的权限)。

从链接中,这是您缺少的东西:

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

对于 AndroidManifest.xml (如果您宁愿只使用不太准确的网络/wifi 位置而不是 GPS,您可能必须更改权限):

<manifest ... >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    ...
</manifest>
于 2011-03-11T17:41:25.863 回答
1

顺便说一句,您可以使用 Engine/BaseGameActivity 类的 enableLocationSensor 让 AndEngine 为您管理位置检索。

于 2011-06-20T11:15:29.937 回答