JS global variables? or proccessing mis-order... [SOLVED]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

JS global variables? or proccessing mis-order... [SOLVED]

Post by J_Iceman05 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a function that requires a second function work properly.
I delcare 2 variables before either function begins.
The first function sends info to the second and the second function is supposed to assign the 2 variables the longitude and latitude of the address sent.  for testing reasons, i do an alert of the variables in both functions.  The second does fine, but the first function doesn't recieve the variables.  To my understanding, if the variable is declared outside of the functions it should be global... but that doesn't seem to be the case.

[syntax="javascript"]
<script type="text/javascript">
//<![CDATA[

var lat;
var lon;

function load() {
   if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        var geocoder = new GClientGeocoder();
        map.setCenter(new GLatLng('38.36626','-095.780260'), 4);
        map.addControl(new GLargeMapControl());
        map.addControl(new GOverviewMapControl());
        map.addControl(new GMapTypeControl());

        // Create our "tiny" marker icon
        var icon = new GIcon();
        icon.image = "http://uppi.biodose.com/mod/phpwsbusinesses/img/both.gif";
        icon.iconAnchor = new GPoint(1, 17);
        icon.infoWindowAnchor = new GPoint(5, 1);
        var icon1 = new GIcon();
        icon1.image = "http://uppi.biodose.com/mod/phpwsbusinesses/img/cyc.gif";
        icon1.iconAnchor = new GPoint(1, 17);
        icon1.infoWindowAnchor = new GPoint(5, 1);
   }
   // Add 10 markers to the map at random locations
   var bounds = map.getBounds();
   var southWest = bounds.getSouthWest();
   var northEast = bounds.getNorthEast();
   var lngSpan = northEast.lng() - southWest.lng();
   var latSpan = northEast.lat() - southWest.lat();
   var point = new Array();

   map.addOverlay(createMarker(new GLatLng('38.36626','-095.780260'), icon1, 'asdf'));

   geocoder.getLocations('3940 South Eastern Ave, Las Vegas, Nevada, 89119, usa', alertAddress); // sends the address to alertAddress function and it shouldset lat and lon to the correct coordinates.
   alert(lat+', '+lon); // ouputs "undefined, undefined"  (quotes not included)
   map.addOverlay(createMarker(new GLatLng(lat, lon), icon, 'asdf')); //doesn't work because lat and lon are undefined.
}

// Creates a marker at the given point with the given number label
function alertAddress(point){
  lat = point.Placemark[0].Point.coordinates[1];
  lon = point.Placemark[0].Point.coordinates[0];
  alert(lat+' | '+lon);// ouputs "36.241234, -115.23421"  (quotes not included)  These are the corrcet coordinates
}

// createMarker function works just fine with everything.
// Creates a marker at the given point with the given number label
function createMarker(point, icon, number) {
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("Marker #<b>" + number + "</b>");
  });
  return marker;
}

load();
//]]>
</script>
I'ld appreciate any help anyone can give me.
If I was confusing or if you need more info, please ask away.
Thanks in advance for everyone's time and effort.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by J_Iceman05 on Thu Jul 27, 2006 12:35 pm, edited 2 times in total.
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

order problem maybe...

Post by J_Iceman05 »

after a little more testing and whatnot... it seems the order of everything is wrong...
I just added another alert below the "load();" line.
it is alert(lat+' = '+lon);

So a total of 3 alerts, the funcion alertAddress containing a pipe separator "|", the one actually in load() which contains a comma separator, and the new one which containes an equals sign as it's separator.

This alert should be performed when the entire load function (and it's functionality) is done... this includes where it calls the "alertAddress()" function.

However, the order in which the alerts appear are first the comma, then the equals sign, and last the pipe.
It should be pipe, comma, equals....

before i added the equal sign one, it still did the comma alert before the pipe alert...

So if that is my problem (the order in which everything is executed) then I could really use some help figuring out why it's doing that...
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

work around found...

Post by J_Iceman05 »

I needed to retrieve the latitude and longitude because i wanted to give the icons my own picture and not the default one. I did however find that I could actually put the whole function in-place of calling it. and put my desired icon where i wanted.

Code: Select all

geocoder.getLocations('3940 South Eastern Ave, Las Vegas, Nevada, 89119, usa', function (point){this.lat = point.Placemark[0].Point.coordinates[1];this.lon = point.Placemark[0].Point.coordinates[0];alert(lat+' | '+lon);map.addOverlay(createMarker(new GLatLng(lat, lon), icon, 'my description'));});
        geocoder.getLocations('3810A Hawthorne Ave, Athens, GA, 30606, USA', function (point){this.lat = point.Placemark[0].Point.coordinates[1];this.lon = point.Placemark[0].Point.coordinates[0];alert(lat+' | '+lon);map.addOverlay(createMarker(new GLatLng(lat, lon), icon1, 'my second descriptoin'));});
hard to read now... but it works.
if you look at the end notice the "map.addOverlay(createMarker(new GLatLng(lat, lon), icon1, 'my second descriptoin'));"
using PHP i can print each of those lines with the address i want. echo out the name for each icon, and what i want the description to be.

The alerts are still in the order as before... but it works anyway.
and yes, even though the 2 giant lines of code come before the alert with a comma separating the lat and lon... that alert and the one with the equals sign still come befor these 2 with pipe separators
Post Reply