【问题标题】:Passing address to Google Maps on page load在页面加载时将地址传递给谷歌地图
【发布时间】:2010-11-11 18:10:11
【问题描述】:

使用地理编码初始化谷歌地图时遇到一些问题。第一个问题是在 $gmap 字符串中使用了任何逗号,第二个问题是获得“gmap_initialize is not defined”。我知道函数之外的一切都是正确的,有什么想法吗?

<?php $gmap = "Prague, Czech Republic"; ?>

<script type="text/javascript">
function gmap_initialize() {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': <?php echo $gmap; ?>}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    var options = {
      zoom: 16,
      position: results[0].geometry.location,
      center: results[0].geometry.location,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), options);

    var marker = new google.maps.Marker({
      map: map,
      position: results[0].geometry.location
    });
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});

}
</script>

【问题讨论】:

    标签: google-maps geocoding


    【解决方案1】:

    你看过脚本的输出了吗?!

    我猜它看起来像:

    geocoder.geocode( { 'address': Prague, Czech Republic}, function(results, status) {

    在您的 PHP 脚本中,您可能实际上想要这样的东西:

    geocoder.geocode( { 'address': '&lt;?php echo $gmap; ?&gt;'}, function(results, status) {

    您可能希望从 $gmap 变量中转义单引号。

    【讨论】:

    • 感谢 Broady,这是解决方法(忘记更新我的答案)。逃跑也不错。
    【解决方案2】:

    这看起来像一个 JavaScript 错误。说这个函数(看起来正确)没有定义。也许对 gmap_initialize 的调用发生在此定义之前?

    【讨论】:

      【解决方案3】:

      通常当页面上的其他内容不完全正确时,JavaScript 会出现此错误,例如缺少单个关闭标记...

      您是否使用 W3C 验证过代码?

      【讨论】:

        猜你喜欢
        • 2016-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-09
        相关资源
        最近更新 更多