【问题标题】:How can I fix this JavaScript syntax error?如何修复此 JavaScript 语法错误?
【发布时间】:2009-07-07 17:50:01
【问题描述】:

这让我很困惑。我正在使用谷歌地图的地理编码来查找位置。我正在尝试使用来自 Google 的示例 here,但它对我不起作用。


错误:

http://maps.gstatic.com/intl/en_us/mapfiles/159e/maps2.api/main.js
174号线
var point = new GLatLng(,);


代码:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='.$config['locations.gMaps.key'].'" type="text/javascript"></script>
<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key='.$config['locations.gMaps.key'].'" type="text/javascript"></script>
<script src="http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js" type="text/javascript"></script>

<style type="text/css">
    @import url("http://www.google.com/uds/css/gsearch.css");
    @import url("http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css");
</style>
<script type="text/javascript">

    function addListener(element, baseName, handler) {
        if (element.addEventListener)
            element.addEventListener(baseName, handler, false);
        else if (element.attachEvent)
            element.attachEvent("on"+baseName,handler);
    }
    
    var map'.$num.';    
    
    function initialize'.$num.'() 
    {
    
        if (GBrowserIsCompatible()) 
        {
            map'.$num.' = new GMap2(document.getElementById("google_map'.$num.'"),{mapTypes:[G_HYBRID_MAP]});
            var point = new GLatLng('.$row->LocationLat.','.$row->LocationLon.');
            map'.$num.'.setCenter(new GLatLng('.$row->LocationLat.','.$row->LocationLon.'),4);
            var mapControl = new GMapTypeControl();
            map'.$num.'.addControl(mapControl);
            map'.$num.'.addControl(new GLargeMapControl());
            map'.$num.'.addControl(new GOverviewMapControl());
            map'.$num.'.enableDoubleClickZoom();
            map'.$num.'.enableScrollWheelZoom();
            var bounds = new GLatLngBounds;
            
            var myIcon = new GIcon();
            myIcon.image = "http://www.google.com/mapfiles/marker.png";
            myIcon.iconAnchor = new GPoint((markerImage1.width/2),markerImage1.height);
            
            
            
            bounds.extend(point);
            setBounds(map'.$num.',bounds);                              
            
            var address = "' . $address . '";
            
            
            var geocoder = new GClientGeocoder();
            showAddress(address, geocoder);
            
        }
    }
    
    function showAddress(address, geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            alert(address + " not found");
          } else {
            map'.$num.'.setCenter(point, 13);
            var marker = new GMarker(point);
            map'.$num.'.addOverlay(marker);
            marker.openInfoWindowHtml(address);
          }
        }
      );
    }

                
    function setBounds(map'.$num.',bounds) 
    {
        map'.$num.'.setZoom(15);
        map'.$num.'.setCenter(bounds.getCenter());
    }                   

    function chargement() 
    {   
        markerImage1 = new Image(); 
        markerImage1.src = "http://www.google.com/mapfiles/marker.png";
        setTimeout("initialize'.$num.'()", 500); 
    }

    addListener(window, "load", chargement);
</script>

我的代码是由 PHP 生成的,所以当有一个 ' 时,表示我正在打开或关闭保存 JavaScript 的字符串。

【问题讨论】:

  • 未找到 在此服务器上未找到请求的 URL /intl/en_us/mapfiles/159e/map2.api/main.js。

标签: javascript google-maps geocoding


【解决方案1】:

也许我没听懂,但是

var point = new GLatLng(,);

不是有效的javascript

应该是

var point = new GLatLng(param1, param2);

var point = new GLatLng();

var point = new GLatLng(null,null);

...取决于 GLatLng 构造函数是什么

【讨论】:

  • 我知道,我无法访问引发错误的 google javascript。
  • oooh...你在哪里调用函数?什么参数?
  • 我假设 geocoder.getLatLng 在函数 showAddress 中调用它
  • 生成调用函数的php好像关闭了
  • 我已经附和了,看起来应该。
【解决方案2】:

此声明:

var point = new GLatLng(,);

不正确,因为没有指定 lat 或 lng 数字。这是因为这句话:

var point = new GLatLng('.$row->LocationLat.','.$row->LocationLon.');

不正确。我会尝试类似:

var point = new GLatLng(<?php echo $row->LocationLat . ',' . $row->LocationLon; ?>);

如果这不起作用,那么$row-&gt;LocationLat$row-&gt;LocationLon 可能为空。

【讨论】:

  • 该点调用已被注释掉。我不使用它。 showAddress 函数的问题。
【解决方案3】:

问题1-函数showAddress()没有关闭。

问题 2 - 您的地图对象需要在函数之外定义,以便showAddress() 可以访问它。

问题 3 - showAddress() 内部对地图对象的引用不正确

【讨论】:

  • 我修复了这三个问题,但仍然抛出同样的错误,将更新代码。
  • @Malfist - 你能告诉我们执行的 php 代码吗?这样我们就可以查看您的 php 输出的内容是否有任何问题。
  • 我只是更改了代码以反映谷歌地图的示例。我不知道为什么其他程序会在其中留下所有其他内容。它从未使用过。现在一切正常。
  • 我不懂你的 PHP 语法。 var map'.$num.'; ??你的意思是使用 var map=$num?>; ?
  • 不,正如我在问题中所说,事物是一个字符串。我只是打开字符串,并插入一个 PHP 变量。然后它将字符串回显到浏览器。
【解决方案4】:

首先检查您打印到 html+js 中的 php 字符串是否存在。 php 生成 htm 并将其发送给用户,现在是 htm+javascript 问题。 它看起来像一个 javascript 问题,但你确实用 php 生成了错误的语法,因为你试图打印一些有问题的东西并且它打印了一个空白空间。 始终小心这一点,确保打印的内容。

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 2023-03-09
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多