.appendText is writing on top of last text without replacing it in GeolocationEvent.UPDATE example

StackOverflow https://stackoverflow.com/questions/15723366

Question

new to actionscript and looking at the GeolocationEvent.UPDATE examples, having some unexpected results with .appendText() and an array.push --I didn't know whether they might both be just the phone not keeping up with the updates?

first, the text problem is that it's overwriting rather than replacing the last write, so after a couple minutes of the app running on the phone, you can't read the numbers any more. --using this.removeChild() and then addChild() was about trying to get it to remove the last write before writing again.

and then second, the problem with the array is that it's outputting random .length numbers in the trace() --the length looks to occasionally reset to 2 before counting up again, and counts up to seemingly random numbers. I know that I don't want the overhead of an array in the final version, but I'm trying to learn from why it's not working.

I've commented out the different things I've tried --sorry if I've missed something basic here

var format:TextFormat = new TextFormat();
    format.color = 0xff0066;
    format.font = "Lucida Console";
    format.size = 20;
var fl_GeolocationDisplay:TextField = new TextField();
    fl_GeolocationDisplay.defaultTextFormat = format;   
    fl_GeolocationDisplay.x = 10; 
    fl_GeolocationDisplay.y = 20;
    fl_GeolocationDisplay.selectable = false;   
    fl_GeolocationDisplay.autoSize = TextFieldAutoSize.LEFT;
//fl_GeolocationDisplay.text = "Geolocation is not responding. Verify the device's     location settings.";
fl_GeolocationDisplay.text = " ";
addChild(fl_GeolocationDisplay);

var gpsArray:Array = [42.09646417];

if(!Geolocation.isSupported)
{
    fl_GeolocationDisplay.text = "Geolocation is not supported on this device.";
}
else
{
    var fl_Geolocation:Geolocation = new Geolocation();
    fl_Geolocation.setRequestedUpdateInterval(60000); //android overrides     setRequestedUpdateInterval()
    fl_Geolocation.addEventListener(GeolocationEvent.UPDATE, fl_UpdateGeolocation);
    fl_Geolocation.addEventListener(StatusEvent.STATUS, gpsStatusHandler);
}

function fl_UpdateGeolocation(event:GeolocationEvent):void
{
    //gpsArray.push(event.latitude);
    //gpsArray[gpsArray.length] = event.latitude;
    gpsArray.unshift(event.latitude);
    var speed:Number = event.speed * 2.23693629; 
    if (gpsArray[gpsArray.length - 2] != gpsArray[gpsArray.length - 1]) 
    {       
        trace(gpsArray.length + "|" + gpsArray[gpsArray.length - 2] + "|" +     gpsArray[gpsArray.length - 1]);
        trace(gpsArray[1] + "|" + gpsArray[0]);
        trace(gpsArray[gpsArray.length - 2] - gpsArray[gpsArray.length - 1]);
    }

    //this.removeChild(fl_GeolocationDisplay);
    fl_GeolocationDisplay.parent.removeChild(fl_GeolocationDisplay);
    //fl_GeolocationDisplay = null; //TypeError: Error #2007: Parameter child must be non-null.
    addChild(fl_GeolocationDisplay);
    fl_GeolocationDisplay.text = (event.latitude.toString() + " | " +     event.timestamp.toString());
    //fl_GeolocationDisplay.text = (event.latitude.toString() + "\n");
    //fl_GeolocationDisplay.appendText(event.latitude.toString() + "\n");
    //fl_GeolocationDisplay.appendText(event.longitude.toString() + "\n");
}

function gpsStatusHandler(event:StatusEvent):void {
    if (fl_Geolocation.muted) {
        fl_GeolocationDisplay.text = "Please verify the device's location     settings.";
    }
}
Était-ce utile?

La solution

I really can't understand what it is that you are trying to do, I mean you say one thing but your code seem to say something different.

There is also a serious issue about where the different code snippets are located? It Seems like the top part is inside a constructor. And then the bottom part are their own functions? If that is the case, make sure that the constructor is not run multiple times (which seems to be the issue and explaining why items are "overwritten" on top of each other.

Also your question states something about appendText but it seems like you want to replace the text inside the textfield? AppendText will add extra text inside that text field.

Anyways, I did an implementation from your code that gets the "longitude|lattitude" from the update event and then appends these to the textfield on a new line. Maybe this is what you wanted to do? I commented out the gps-array since I had no idea what it was that you tried to achieve by doing this:

package {
    import flash.events.GeolocationEvent;
    import flash.events.StatusEvent;
    import flash.sensors.Geolocation;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;


    public class Foobar extends MovieClip {
        var gpsArray:Array = [42.09646417];
        var format:TextFormat = new TextFormat();
        var fl_GeolocationDisplay:TextField = new TextField();
        var fl_Geolocation:Geolocation = new Geolocation();

        public function Foobar() {
            format.color = 0xff0066;
            format.font = "Lucida Console";
            format.size = 20;
            fl_GeolocationDisplay.defaultTextFormat = format;
            fl_GeolocationDisplay.x = 10;
            fl_GeolocationDisplay.y = 20;
            fl_GeolocationDisplay.selectable = false;
            fl_GeolocationDisplay.autoSize = TextFieldAutoSize.LEFT;
            //fl_GeolocationDisplay.text = "Geolocation is not responding. Verify the device's     location settings.";
            fl_GeolocationDisplay.text = " ";
            addChild(fl_GeolocationDisplay);


            if(!Geolocation.isSupported) {
                trace("unsupported");
                fl_GeolocationDisplay.text = "Geolocation is not supported on this device.";
            } else {
                trace("supported");
                fl_Geolocation.setRequestedUpdateInterval(500); //android overrides     setRequestedUpdateInterval()
                fl_Geolocation.addEventListener(GeolocationEvent.UPDATE, fl_UpdateGeolocation);
                fl_Geolocation.addEventListener(StatusEvent.STATUS, gpsStatusHandler);
            }
        }

        function fl_UpdateGeolocation(event:GeolocationEvent):void {
            /*gpsArray.unshift(event.latitude);
            var speed:Number = event.speed * 2.23693629;
            if (gpsArray[gpsArray.length - 2] != gpsArray[gpsArray.length - 1]) {
                trace(gpsArray.length + "|" + gpsArray[gpsArray.length - 2] + "|" +     gpsArray[gpsArray.length - 1]);
                trace(gpsArray[1] + "|" + gpsArray[0]);
                trace(gpsArray[gpsArray.length - 2] - gpsArray[gpsArray.length - 1]);
            }*/

            fl_GeolocationDisplay.appendText(event.latitude.toString() + "|" + event.longitude.toString() + "\n");
        }

        function gpsStatusHandler(event:StatusEvent):void {
            if (fl_Geolocation.muted) {
                fl_GeolocationDisplay.text = "Please verify the device's location     settings.";
            }
        }
    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top