I'm having a hard time with my first javascript class... when I attempt to populate this.locations with the geocode() method, it doesn't work. I know that it is finding the locations fine because I tested the return value of getLocations() inside the geocode() method and they were all there. But then when I try to access them by this.locations they aren't there
function geoLocation(form, map) {
this.form = form;
this.map = map;
this.address = form.address1.value + " " + form.city.value + ", " + form.state.value + " " + form.zip.value;
this.geocoder = new GClientGeocoder;
this.locations = null;
this.find = find;
this.geocode = geocode;
this.find();
}
function find() {
this.geocoder.getLocations(this.address, this.geocode);
alert(this.locations); // alerts a null value
}
// For some reason this method doesn't seem to be populating this.locations
function geocode(response) {
this.statusCode = response.Status.code;
if(this.statusCode == 200){
this.locations = response.Placemark;
// alert(this.locations); // alerts me with some locations it found
}
}
EDIT: I fiddled with it a little more and came to realize that the geocode function is behaving as if it is a function on its own rather than a method of the getLocation class... why would it be doing that? Could it have something to do with the fact that I am using it as a callback function?
The Ninja Space Goat wrote:
EDIT: I fiddled with it a little more and came to realize that the geocode function is behaving as if it is a function on its own rather than a method of the getLocation class... why would it be doing that? Could it have something to do with the fact that I am using it as a callback function?
function MyGeocoder(address){
this.locations;
this.geocoder = new GClientGeocoder;
// Populate locations with my finder method
this.geocoder.getLocations(address, this.find); // This is telling me that this.find isn't a function
this.find = function(response){
alert(response);
}
}
This doesn't work... it's telling me that this.find isn't a function...
function MyGeocoder(address){
this.locations;
this.geocoder = new GClientGeocoder;
// Populate locations with my finder method
this.geocoder.getLocations(address, this.find); // This is telling me that this.find isn't a function
this.find = function(response){
alert(response);
}
}
This doesn't work... it's telling me that this.find isn't a function...
Oh wait is that constructor logic? Stick it down at the bottom so that it runs after the methods inside it have been defined.
function MyGeocoder(address){
this.locations;
this.geocoder = new GClientGeocoder;
this.find = function(response){
alert(response);
}
// Populate locations with my finder method
this.geocoder.getLocations(address, this.find);
}
Last edited by Chris Corbyn on Wed Sep 06, 2006 12:29 pm, edited 1 time in total.
what is your way? I've never seen that before... I will be more than happy to try it... but I would like to understand it before just plopping it into my code... can you explain?
function MyGeocoder(address){
this.locations;
this.geocoder = new GClientGeocoder;
this.find = find;
// Populate locations with my finder method
this.geocoder.getLocations(address, this.find);
function find(response){
this.locations = response;
alert(this.locations); // alerts "[object Object]"
}
}
function loadAddress(){
var address = '5047 Arden Way, Paradise, CA 95969';
var Geocode = new MyGeocoder(address);
alert(Geocode.locations); // alerts "undefined"
}
function MyGeocoder(address){
this.locations;
this.geocoder = new GClientGeocoder;
this.find = function(response){
this.locations = response;
alert(this.locations); // alerts "[object Object]"
}
// Populate locations with my finder method
this.geocoder.getLocations(address, this.find);
}
function loadAddress(){
var address = '5047 Arden Way, Paradise, CA 95969';
var Geocode = new MyGeocoder(address);
alert(Geocode.locations); // alerts "undefined"
}
Object literal... I will have to put that on the research list...
I think that the fact that it is being used as a callback function is messing up its state or something... it's like it has readonly access to all of the objects properties. It can read them, but changing them only effects them with in that particular function... nowhere else... I am getting pretty frustrated
Callbacks between objects usually work from what I remember. Let's try it Might post back but someone's entising me to go downstairs and eat my dinner (thai green curry)