繁体   English   中英

如何解决此JavaScript语法错误?

[英]How can I fix this JavaScript syntax error?

这让我感到困惑。 我正在使用Google Map的地址编码来查找位置。 我正在尝试使用此处的示例,该示例来自Google,但对我不起作用。


错误:

http://maps.gstatic.com/intl/zh_CN/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的字符串。

也许我不明白,但是

var point = new GLatLng(,);

无效的javascript

应该是

var point = new GLatLng(param1, param2);

要么

var point = new GLatLng();

要么

var point = new GLatLng(null,null);

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

这个说法:

var point = new GLatLng(,);

不正确,因为未指定纬度或经度编号。 这是因为此语句:

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

是不正确的。 我会尝试类似的东西:

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

如果这不起作用,则$row->LocationLat$row->LocationLon可能为空。

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

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

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

首先检查要打印到html + js中的php字符串。 php生成htm并将其发送给用户,目前是htm + javascript问题。 它看起来像是一个Javascript问题,但您实际上一开始就用php生成了错误的语法,因为您尝试打印有问题的内容,并且打印了空白。 请始终注意这一点,请确保打印出什么内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM