Javascript and ActionScript 3 [Setting Problem Variables]
Ok, so I am trying to set a variable using a javascript method call. However, this doesn't work. I googled and ended up ip by going back and trying swLiveConnect but this is deprecated and it turns out my way is now a way to do it. So here's my javascript and ActionScript function. I have no idea what's going on, I bow to the more experienced instructors.
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 sure it has something to do with the return, but I'm not sure. At this point, I tried to keep track of which address is passed to the function, that's correct. I tracked the distance set before and after the call. Before that, NaN; after, 0. From Firebug I get the correct number. I just do not know.
a source to share
I'm not sure about this, but ... 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;
}
a source to share
The problem is that it geocoder.getLocations
doesn't return the return value of the function you passed to it. You can try using closure:
function showLocation(address) {
var retval
geocoder.getLocations(address, function (response) {
... // don't use another 'var retval' here
reval = ...
}
return retval
}
a source to share