0

我正在使用 Ultimatum WordPress 主题模板中的以下代码以及一些 PHP 代码来提供地图所需的动态值。我收到标记未定义错误。代码位于页面下方 3/4 的 div 标签内,而不是放在页眉或页脚中,因为它只用于一页。我尝试了一些没有运气的建议。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
var geocoder;
var map;
function initialize ()
{
    geocoder = new google.maps.Geocoder();
    var address = document.getElementById('Property').value;
    var city = document.getElementById('City').value;
    var postal_code = document.getElementById('PostalCode').value;
    var address_google_map = address + ', ' + city + ', ON ' + postal_code;

    var myOptions =
    {
        zoom: 15,
        mapTypeControl: true,
        zoomControl: true,
        zoomControlOptions:
        {
            style: google.maps.ZoomControlStyle.SMALL
        },
        StreetViewControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var info_text = address + '<br />' + city + ' ON';
    var info_window = new google.maps.InfoWindow
    ({
        content: info_text
    });
    var map = new google.maps.Map(document.getElementById('google_map'), myOptions);

    geocoder.geocode( { 'address': address_google_map}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });


    google.maps.event.addListener(marker, 'click', function()
    {
        info_window.open(map, marker);
    });
}


window.onload = function() {
   initialize();
}
//]]>
</script>
4

1 回答 1

3

标记是 Geocoder 回调例程的本地标记。将单击侦听器定义移动到函数内的范围内。

geocoder.geocode( { 'address': address_google_map}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });

    google.maps.event.addListener(marker, 'click', function()
    {
    info_window.open(map, marker);
    });
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});

}

于 2013-05-24T05:38:27.807 回答