Page 2 of 2

Posted: Wed Sep 06, 2006 5:02 pm
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...

Posted: Wed Sep 06, 2006 5:03 pm
by Luke
Ooops... I guess it was. :oops: I didn't mean to though... :P

Posted: Wed Sep 06, 2006 5:54 pm
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);
   }

Posted: Wed Sep 06, 2006 8:05 pm
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).

Posted: Wed Sep 06, 2006 10:04 pm
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"

Posted: Wed Sep 06, 2006 10:43 pm
by nickvd
I don't see UCA.locations being defined.

Posted: Thu Sep 07, 2006 12:17 am
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 :)

Posted: Thu Sep 07, 2006 3:39 am
by Weirdan
var me = this; // rename this
you're supposed to move that line to constructor

Posted: Thu Sep 07, 2006 12:56 pm
by daedalus__
Ninja, you should look up JSON if you haven't already.

Posted: Thu Sep 07, 2006 1:05 pm
by Luke
see the response variable? That's a JSON object :)

Posted: Thu Sep 07, 2006 1:50 pm
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.

Posted: Thu Sep 07, 2006 2:05 pm
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?