最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - can the callback from GetLocations() (from the Google Maps API) call anything other than API functions? - Stack Ove

programmeradmin0浏览0评论

I'm working in Javascript on a Maps API project for work. I'm having trouble with the callback function which I pass to GetLocations in which it has to call another chunk of code (all the callback does is store the lat and lng into an object). But after the function does its work, the next function doesn't get called.

How does this callback work? Why can't I call any functions from it? What can I do in it?

update

Well the test function (which just made an alert box) I was using started magically working again, and I checked the error log (which I forgot existed before) to see what was going wrong.

The Javascript is using the prototype framework to do some kind of OO and the function that has to get called is "this.Create". The error said there's no such function, but it lets me call it from another place in the code:

for (var i=0;i<mapObjects.length;i++) {
    mapObjects[i] = new mapObject(mapObjects[i]);
    mapObjects[i].Create(); //this works
}



mapObject.prototype.SetLocation=function (response) {
    this.geoStatusCode = response.Status.code;
    alert("entered SetLocation with status code "+this.geoStatusCode);
    if (this.geoStatusCode == 200) {
        this.lat = response.Placemark[0].Point.coordinates[1];
        this.lng = response.Placemark[0].Point.coordinates[0];
        alert("calling create()");
        this.Create(); //"no such function"
    } else {
        this.geofailed++;
    }

}

I'm not really familiar with Javascript, and don't really understand prototype or how it works, so I have no idea how to solve this. Anyone know?

I'm working in Javascript on a Maps API project for work. I'm having trouble with the callback function which I pass to GetLocations in which it has to call another chunk of code (all the callback does is store the lat and lng into an object). But after the function does its work, the next function doesn't get called.

How does this callback work? Why can't I call any functions from it? What can I do in it?

update

Well the test function (which just made an alert box) I was using started magically working again, and I checked the error log (which I forgot existed before) to see what was going wrong.

The Javascript is using the prototype framework to do some kind of OO and the function that has to get called is "this.Create". The error said there's no such function, but it lets me call it from another place in the code:

for (var i=0;i<mapObjects.length;i++) {
    mapObjects[i] = new mapObject(mapObjects[i]);
    mapObjects[i].Create(); //this works
}



mapObject.prototype.SetLocation=function (response) {
    this.geoStatusCode = response.Status.code;
    alert("entered SetLocation with status code "+this.geoStatusCode);
    if (this.geoStatusCode == 200) {
        this.lat = response.Placemark[0].Point.coordinates[1];
        this.lng = response.Placemark[0].Point.coordinates[0];
        alert("calling create()");
        this.Create(); //"no such function"
    } else {
        this.geofailed++;
    }

}

I'm not really familiar with Javascript, and don't really understand prototype or how it works, so I have no idea how to solve this. Anyone know?

Share Improve this question edited Jul 15, 2009 at 0:29 Carson Myers asked Jul 14, 2009 at 5:52 Carson MyersCarson Myers 38.6k41 gold badges131 silver badges183 bronze badges 2
  • 1 I think your code is using Javascript's prototype property, not the prototype framework. Where you see prototype in your code above, you are adding the SetLocation() function to mapObject. There's a bit more to your code going on than I can see here. Can you post more of it? – Chris B Commented Jul 15, 2009 at 15:24
  • oh, I didn't know there was a difference between the property and the framework. The code includes prototype.js though, so I think it uses the framework as well. I would post more code but I have changed it so much by now--I restructured it and eventually got it to work. Thanks for the help though. – Carson Myers Commented Jul 18, 2009 at 1:46
Add a ment  | 

2 Answers 2

Reset to default 3

You can do whatever you like from the callback function. Google's documentation has a great example of how to use the Geocoder. I expanded their example slightly:

var map;
var geocoder = new GClientGeocoder();

function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("\"" + address + "\" not found");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br><b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
        // DO WHATEVER YOU WANT HERE
    }
}

geocoder.getLocations("New York City", addAddressToMap);

Alternatively, you can print the callback function inline:

var map;
var geocoder = new GClientGeocoder();

geocoder.getLocations(address, function() {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("\"" + address + "\" not found");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br><b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
        // DO WHATEVER YOU WANT HERE
    }
});

You can call other javaScript functions from the GetLocations callback. I suggest you load your page in Firefox and check the error log. If your problems persist, please post the code here and hopefully we can help.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论