Can't populate member variable in javascript

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

I am sorry to say but currently I don't have Google API code/key so I am unable to help you further on this...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Ooops... I guess it was. :oops: I didn't mean to though... :P
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

You may want to try the geocoding script i've been using for clients sites, while not 100% oop'ed up, it works! :)

parseQueryString() just does what it says, if you need it, just lemme know and i'll post it.

window.onload should call load()

Code: Select all

   var query = parseQueryString();
   var map,geocoder,centre,marker;
   var mapData = {
      def: {
         addr: '76 Main St East, Grimsby, On',
         caption: 'The Main Office For eCompulab, located Just <br/>off the highway in scenic grimsby, ontario'
      },
      one: {
         addr: '181 South Service Rd., Grimsby, On',
         caption: 'Our Secondary Office, Located Just off the highway<br/> in scenic grimsby, ontario'
      },
      two: {
         addr: '21 Main St. E. Grimsby, On',
         caption: 'Our Retail Store, Located in the Downdown city<br/> centre in scenic grimsby, ontario',
         lat: 43.192542,
         lng: -79.559653
      }
   };

   function load() {
      curLoc = (mapData[query.loc])?mapData[query.loc]:mapData.def;
      if (GBrowserIsCompatible() && curLoc) {
         document.title += ' '+curLoc.addr;
         if (!curLoc.lat || !curLoc.lng) {
            geocoder = new GClientGeocoder();
            geocoder.getLocations(curLoc.addr, function(response){
               if (!response || response.Status.code != 200) {
                  alert("Sorry, we were unable to geocode that address");
               } else {
                  place = response.Placemark[0];
                  centre = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                  marker = new GMarker(centre);
                  loadMap(centre,marker,{addr:place.address,caption:curLoc.caption});
               }
            });
         } else {
            centre = new GLatLng(curLoc.lat, curLoc.lng);
            marker = new GMarker(centre);
            loadMap(centre,marker,{addr:curLoc.addr,caption:curLoc.caption});
         }
      }
   }
   function loadMap(centre,marker,place) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(centre, 15);
      map.addOverlay(marker);
      html = '<strong>'+place.addr+'</strong>';
      if (place.caption) html += '<br/>'+place.caption;
      marker.openInfoWindowHtml(html);
   }
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

The Ninja Space Goat wrote:Still not working...

I think that the fact that it is being used as a callback function is messing up its state or something...
try to 'rename' this variable like:

Code: Select all

function objectConstructor() {
    var me = this; // <======== magic :))
    this.data = [];
    this.callbackFunction = function(arg) {
          me.data.push(arg);
    }
}
some objects have a nasty habit of overriding this variable passed to callback function with itself (DOM objects are notable example).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

	function UCA(){
		// TODO: Find a better way to encapsulate this stuff
		var street 	= $F('street');
		var city 	= $F('city');
		var state 	= $F('state');
		var zip 	= $F('zip');
		var address = street + ", " + city + ", " + state + ", " + zip;

		this.locations;
		this.geocoder = new GClientGeocoder;
		this.geocoder.getLocations(address, this.handleResponse);
		this.handleResponse = function(response){
			var me = this;
			if(response.Status.code == 200){
				me.locations = response.Placemark;
			}
		}
		alert(this.locations); // alerts "undefined"
	}
and the button:

Code: Select all

<input type="button" value="Locate" onclick="var loc = new UCA(); alert(loc.locations);" />
This alerts "undefined"
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I don't see UCA.locations being defined.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

	function UCA(){
		// TODO: Find a better way to encapsulate this stuff
		var street 	= $F('street');
		var city 	= $F('city');
		var state 	= $F('state');
		var zip 	= $F('zip');
		var address = street + ", " + city + ", " + state + ", " + zip;

		this.locations;
		this.geocoder = new GClientGeocoder;
		this.geocoder.getLocations(address, this.handleResponse); // This calls the handle response function
		this.handleResponse = function(response){
			var me = this; // rename this
			if(response.Status.code == 200){ // I know this is true, I tested it
				me.locations = response.Placemark; // and if I alert(me.locations) I get the expected result
			}
		}
		alert(this.locations); // but now down here it's undefined again
	}
OK I added notes that may be able to help you help me :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

var me = this; // rename this
you're supposed to move that line to constructor
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Ninja, you should look up JSON if you haven't already.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

see the response variable? That's a JSON object :)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

It is. Sorry that I didn't see that.

I just saw this a few times and decided to mention it.

Code: Select all

// TODO: Find a better way to encapsulate this stuff
I just found out about this JSON stuff the other day.
asgerhallas
Forum Commoner
Posts: 80
Joined: Tue Mar 14, 2006 11:11 am
Location: Århus, Denmark

Post by asgerhallas »

I too think it's something about you using it as callback... This UCA() how do you call it, to get the last alert, which you say is undefined?
Post Reply