Can't populate member variable in javascript
Moderator: General Moderators
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
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()
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);
}try to 'rename' this variable like: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...
Code: Select all
function objectConstructor() {
var me = this; // <======== magic :))
this.data = [];
this.callbackFunction = function(arg) {
me.data.push(arg);
}
}
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"
}
Code: Select all
<input type="button" value="Locate" onclick="var loc = new UCA(); alert(loc.locations);" />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
}
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
It is. Sorry that I didn't see that.
I just saw this a few times and decided to mention it.
I just found out about this JSON stuff the other day.
I just saw this a few times and decided to mention it.
Code: Select all
// TODO: Find a better way to encapsulate this stuff-
asgerhallas
- Forum Commoner
- Posts: 80
- Joined: Tue Mar 14, 2006 11:11 am
- Location: Århus, Denmark