【问题标题】:can the callback from GetLocations() (from the Google Maps API) call anything other than API functions?GetLocations()(来自 Google Maps API)的回调可以调用 API 函数以外的任何东西吗?
【发布时间】:2010-11-10 13:17:33
【问题描述】:

我正在使用 JavaScript 开发 Maps API 项目。我在传递给GetLocations 的回调函数时遇到问题,它必须调用另一块代码(回调所做的只是将 lat 和 lng 存储到一个对象中)。但是在函数完成其工作后,下一个函数不会被调用。

这个回调是如何工作的?为什么我不能从中调用任何函数? 可以在里面做什么?

更新

嗯,我正在使用的测试功能(它只是一个警告框)再次神奇地开始工作,我检查了错误日志(我之前忘记了它)看看出了什么问题。

Javascript 正在使用原型框架来执行某种 OO,必须调用的函数是“this.Create”。错误说没有这样的函数,但它让我可以从代码中的另一个地方调用它:

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++;
    }

}

我对 Javascript 不是很熟悉,也不是很了解原型或它是如何工作的,所以我不知道如何解决这个问题。有人知道吗?

【问题讨论】:

  • 我认为您的代码使用的是 Javascript 的原型属性,而不是原型框架。您在上面的代码中看到原型的地方,就是将 SetLocation() 函数添加到 mapObject。你的代码比我在这里看到的要多一些。能多发点吗?
  • 哦,我不知道属性和框架之间有区别。代码虽然包含prototype.js,所以我认为它也使用了框架。我会发布更多代码,但我现在已经对其进行了很大的更改——我对其进行了重组并最终让它工作。不过感谢您的帮助。

标签: javascript api google-maps callback


【解决方案1】:

你可以通过回调函数做任何你喜欢的事情。 Google 的文档有一个great example 说明如何使用地理编码器。我稍微扩展了他们的例子:

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);

或者,您可以内联打印回调函数:

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
    }
});

【讨论】:

    【解决方案2】:

    您可以从 GetLocations 回调中调用其他 javaScript 函数。我建议您在 Firefox 中加载您的页面并检查错误日志。如果您的问题仍然存在,请在此处发布代码,希望我们能提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2018-04-15
      • 1970-01-01
      • 2022-12-09
      • 2016-03-08
      相关资源
      最近更新 更多