admin管理员组文章数量:1415139
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
2 Answers
Reset to default 3You 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.
本文标签:
版权声明:本文标题:javascript - can the callback from GetLocations() (from the Google Maps API) call anything other than API functions? - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745210560a2647858.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论