Javascript和Actionscript 3 [设置变量问题]

时间:2021-12-30 22:38:24

Okay, so I'm trying to set a variable via a javascript method call. However this is not working. I googled and ended ip going back and trying swLiveConnect, but that's outdated and it turns out my way is now the way to do it. So here's my javascript function and actionscript. I have no idea what's going on, I bow before the more skilled actionscripters.

好的,所以我试图通过javascript方法调用设置一个变量。但是这不起作用。我用谷歌搜索并结束了IP回去尝试swLiveConnect,但这已经过时了,事实证明我的方式现在是这样做的。所以这是我的javascript函数和actionscript。我不知道发生了什么,我在更熟练的行为文章之前鞠躬。

Actionscript

//==========================================================
function decide_proposal(e:Event):void {
//==========================================================
    var address1:String = form1.project_address.text + ", " + form1.project_city.text + ", " + form1.project_state.text + ", " + form1.project_zip.text;
    var distance:Number = ExternalInterface.call("showLocation(" + address1 + ")");

    var commit:String = e.currentTarget.name;
    if (form1.stories.value >= 2.5 || form1.wood_no.selected || form1.framed_sqft.value >= 5000 || form1.roof_slope.value < 3 || form1.civil.selected || form1.cmt.selected || form1.commercial.selected || form1.c.selected || distance >= 180 || form1.bore_holes1.value >= 4) {
      send_proposal(e);
    } else if (commit == "quote") {
      perform_calculations(distance);
    } else {
      send_proposal(e);
    }
}

Javascript

var geocoder;
var location1;


function initialize() {
    geocoder = new GClientGeocoder();
}
function showLocation(address) {
    var locations = 0;
    geocoder.getLocations(address, function (response) {
        if (!response || response.Status.code != 200)
        {
            alert("There was an error");
        }
        else
        {
            location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0]};
        }
    });
    var glatlng1 = new GLatLng(location1.lat, location1.lon);
    var brenham = new GLatLng(30.152289,-96.384881);
    var college_station = new GLatLng(30.610584,-96.306002);
    var miledistance1 = glatlng1.distanceFrom(brenham);
    var miledistance2 = glatlng1.distanceFrom(college_station);

    if (miledistance1 <= miledistance2) {
        locations = (miledistance1 * .000621371192);
    } else {
        locations = (miledistance2 * .000621371192);
    }
    return locations;
}
window.onload=function() {initialize();}

I'm fairly certain it has something to do with return, but am not sure. At this point I have tried tracing what address is getting passed to the function, it is correct. I have traced what the distance is set to before and after the call. Before, NaN; after, 0. From Firebug I get the correct number. I just don't know.

我很确定它与回归有关,但我不确定。此时我已经尝试跟踪传递给函数的地址,这是正确的。我已经跟踪了呼叫之前和之后距离的设置。之前,NaN;之后,0。从Firebug我得到正确的号码。我只是不知道。

4 个解决方案

#1


Make sure you have allowScriptAccess="always" in the HTML object/embed code.

确保HTML对象/嵌入代码中有allowScriptAccess =“always”。

#2


I'm not really sure about that but... give it a try:

我不是很确定,但是......尝试一下:

function showLocation(address) {
    var locations = geocoder.getLocations(address, function (response) {
        if (!response || response.Status.code != 200)
        {
                alert("There was an error");
        }
        else
        {
                location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0]};
                var glatlng1 = new GLatLng(location1.lat, location1.lon);
                var brenham = new GLatLng(30.152289,-96.384881);
                var college_station = new GLatLng(30.610584,-96.306002);
                var miledistance1 = glatlng1.distanceFrom(brenham);
                var miledistance2 = glatlng1.distanceFrom(college_station);

                if (miledistance1 <= miledistance2) {
                        return (miledistance1 * .000621371192);
                } else {
                        return (miledistance2 * .000621371192);
                }
        }
    });
    return locations;
}

#3


The issue appears to be that geocoder.getLocations is not returning the return value of the function you passed to it. You could try using a closure:

问题似乎是geocoder.getLocations没有返回传递给它的函数的返回值。您可以尝试使用闭包:

function showLocation(address) {
    var retval
    geocoder.getLocations(address, function (response) {
                                       ... // don't use another 'var retval' here
                                       reval = ...
                                   }
    return retval
}

#4


Okay the problem was simple. I was passing the parameters to the function incorrectly. Here's how you do it:

好的,问题很简单。我错误地将参数传递给函数。这是你如何做到的:

    var distance:Number = ExternalInterface.call("showLocation", address1);

Annnnd Bob's your uncle.

Annnnd Bob是你的叔叔。

#1


Make sure you have allowScriptAccess="always" in the HTML object/embed code.

确保HTML对象/嵌入代码中有allowScriptAccess =“always”。

#2


I'm not really sure about that but... give it a try:

我不是很确定,但是......尝试一下:

function showLocation(address) {
    var locations = geocoder.getLocations(address, function (response) {
        if (!response || response.Status.code != 200)
        {
                alert("There was an error");
        }
        else
        {
                location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0]};
                var glatlng1 = new GLatLng(location1.lat, location1.lon);
                var brenham = new GLatLng(30.152289,-96.384881);
                var college_station = new GLatLng(30.610584,-96.306002);
                var miledistance1 = glatlng1.distanceFrom(brenham);
                var miledistance2 = glatlng1.distanceFrom(college_station);

                if (miledistance1 <= miledistance2) {
                        return (miledistance1 * .000621371192);
                } else {
                        return (miledistance2 * .000621371192);
                }
        }
    });
    return locations;
}

#3


The issue appears to be that geocoder.getLocations is not returning the return value of the function you passed to it. You could try using a closure:

问题似乎是geocoder.getLocations没有返回传递给它的函数的返回值。您可以尝试使用闭包:

function showLocation(address) {
    var retval
    geocoder.getLocations(address, function (response) {
                                       ... // don't use another 'var retval' here
                                       reval = ...
                                   }
    return retval
}

#4


Okay the problem was simple. I was passing the parameters to the function incorrectly. Here's how you do it:

好的,问题很简单。我错误地将参数传递给函数。这是你如何做到的:

    var distance:Number = ExternalInterface.call("showLocation", address1);

Annnnd Bob's your uncle.

Annnnd Bob是你的叔叔。