如何在Flash Builder中存储GPS坐标?

时间:2013-03-14 17:54:11

标签: actionscript-3 flash-builder

我正在开发一个应用程序,我需要将所有GPS坐标存储在数据库中。我可以存储它,但只能存储最后一个坐标。

我的问题是,如何存储用户活动的所有坐标?我正在使用WAMP服务器。这是我的代码,它只存储最后的坐标。注意我只使用此代码存储Long和Lat

protected function onUpdate(event:GeolocationEvent):void {
  trace("Update event called");
  log.text = "latitude = " + event.latitude +
    "\nlongitude = " + event.longitude +
    "\naltitude = " + event.altitude +
    "\nverticalAccuracy = " + event.verticalAccuracy +
    "\nhorizontalAccuracy = " + event.horizontalAccuracy +
    "\nspeed = " + event.speed +
    "\nheading = " + event.heading +
    "\ntimestamp = " + event.timestamp;
  lat.text="" + event.latitude;
  long.text="" + event.longitude;
  langTextInput.text = long.text
  latTextInput.text=lat.text
  gpstableService.commit();

1 个答案:

答案 0 :(得分:0)

我认为您需要问自己的第一个问题是您是想在本地存储此坐标还是在WAMP服务器上存储。

如果您只需要本地,那么一个简单的选择是使用本地SharedObject(例如,您可以存储任何新位置,并将您收到事件的日期作为键)。 如果你真的想使用本地数据库,那么你可以使用像这样的服务:

package
{
    import flash.data.SQLConnection;
    import flash.data.SQLStatement;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.SQLEvent;

    public class DatabaseService extends EventDispatcher
    {
        public var conn:SQLConnection;

        public function DatabaseService()
        {
            conn = new SQLConnection();
            conn.addEventListener(SQLEvent.OPEN, handleConnectionSuccess);
            conn.openAsync(null);
        }

        public function addCoordinates(lat:String, long:String):void
        {
            var insertStmt:SQLStatement = new SQLStatement();
            insertStmt.sqlConnection = conn;
            var sql:String = "INSERT INTO coordinates (latitude, longitude)" +
                " VALUES (" + lat + ", " + long + ")";
            insertStmt.text = sql;
            insertStmt.addEventListener(SQLEvent.RESULT, handleInsertionResult);
            insertStmt.execute();
        }

        private function createTable():void
        {
            var createStmt:SQLStatement = new SQLStatement();
            createStmt.sqlConnection = conn;
            var sql:String = "CREATE TABLE IF NOT EXISTS coordinates " +
                "( coordId INTEGER PRIMARY KEY AUTOINCREMENT, latitude TEXT, longitude TEXT)";
            createStmt.text = sql;
            createStmt.addEventListener(SQLEvent.RESULT, handleTableCreationResult);
            createStmt.execute();
        }

        private function handleConnectionSuccess(event:SQLEvent):void
        {
            createTable();
        }

        private function handleTableCreationResult(event:SQLEvent):void
        {
            dispatchEvent(new Event(Event.COMPLETE));
        }

        private function handleInsertionResult(event:SQLEvent):void
        {
            var stmt:SQLStatement = event.target as SQLStatement;
            stmt.removeEventListener(SQLEvent.RESULT, handleInsertionResult);
            trace ("Coordinates are saved in DB");
        }
    }
}

每次收到GPS事件时,您都可以拨打此服务:

    protected function onUpdate(event:GeolocationEvent):void
    {
        trace("Update event called");
        gpsTableService.addCoordinates(event.latitude, event.longitude);
    }

其中gpsTableServiceDatabaseService的实例。

如果您需要在服务器上发送此信息,那么您应编写PHP脚本来处理您的请求。该脚本将负责数据库管理,即将数据存储到其中。要从Flash应用程序调用此脚本,您需要为用户设置URLVariableURLRequestURLLoader