0

我正在从事从 google maps V2 到 V3 的迁移项目,并编写了以下代码,但出现错误并且无法解决问题。

我是否使用了错误的谷歌地图方法?

这段代码有什么问题?

<div id="map_canvas" style="width: 300px; height: 225px; font-family:arial; font-size:10px;"></div>
<?php
$map = getMap();
echo $map = str_replace('$_ADDRESS', 'MYADDRESS', $map );


function getMap()
{
    $mapKey = 'MYKEY';
    $_script = '
        <script type="text/javascript" src="//maps.googleapis.com/maps/api/js?key='. $mapKey .'&sensor=false"></script>
        <script type="text/javascript">
            //<![CDATA[
            var map = null;
            var geocoder = null;

            // call initialize function
            initialize( $_ADDRESS );

            // initialize map

            function initialize() 
            {
                var map = new google.maps.Map(document.getElementById("map_canvas"), {
                center: new google.maps.LatLng(37.4419, -122.1419),
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
                });             
                geocoder = new google.maps.Geocoder();

                // call show Address
                showAddress( address );
            }

            // show address
            function showAddress(address)
            {
                if (geocoder)
                {
                    geocoder.getPosition( address, function(point)
                    {
                        if (!point)
                        {
                            alert(address + " not found");
                        }
                        else
                        {
                            map.setCenter(point, 13);
                            var marker = new google.maps.Marker(point);
                            map.addOverlay(marker);
                            //marker.openInfoWindowHtml(address);
                        }
                    });
                }
            }
            //]]>
    </script>';
    return $_script;
}
?>

有什么想法吗?谢谢

4

2 回答 2

1

我将这些答案拆分为第一个处理 javascript 的基础知识,然后处理使用 Google Maps API。

由于我从未使用过地图 API,因此我无法对 V2 发表评论,但看看您在 V3 中的工作方式,我认为这可以满足您的需求......

<div id="map_canvas" style="width: 300px; height: 225px; font-family:arial; font-size:10px;"></div>
<?php
$map = getMap();
echo $map = str_replace('$_ADDRESS', 'MYADDRESS', $map );


function getMap()
{
    $mapKey = 'MYKEY';
    $_script = '
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places"></script>
        <script type="text/javascript">
            //<![CDATA[
            var map = null;
            var geocoder = null;

            // call initialize function
            initialize( "$_ADDRESS" );

            // initialize map
            function initialize( address ) 
            {
                map = new google.maps.Map(document.getElementById("map_canvas"), {
                center: new google.maps.LatLng(37.4419, -122.1419),
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
                });             
                geocoder = new google.maps.Geocoder();

                // call show Address
                showAddress( address );
            }

            // show address
            function showAddress(address)
            {
                if (geocoder)
                {
                    geocoder.geocode( { "address": address }, function( results, status )
                    {
                        if ( status == google.maps.GeocoderStatus.OK )
                        {
                            position = results[0].geometry.location;
                            map.setCenter(position, 13);
                            var marker = new google.maps.Marker({ map: map, position: position });
                        }
                        else
                        {
                            alert(address + " not found");
                        }
                    });
                }
            }
            //]]>
    </script>';
    return $_script;
}
?>

话虽如此,我会直接对 javascript 中的 str_replace 提出质疑 - 你能相信该数据的来源吗?如果没有,那么您应该在将其放入 javascript 之前查看如何清理该字符串,或者您可能允许人们将代码注入您的网站。

于 2013-11-13T14:02:51.050 回答
0

查看您遇到的基本 javascript 问题...

尝试在要替换到 javascript 中的字符串周围添加引号

echo $map = str_replace('$_ADDRESS', 'MYADDRESS', $map );

变成:

echo $map = str_replace('$_ADDRESS', "'MYADDRESS'", $map );

这样,正确引用了包含 javascript 中地址的字符串。

此外,请确保您可以在初始化函数中接收地址:

function initialize() 

变成:

function initialize( address ) 

最后,当你在“initialize”中为它赋值时不要重新声明“map”,否则你引用了一个只存在于该函数中的不同变量:

var map = new google.maps.Map(document.getElementById("map_canvas"), {

变成:

map = new google.maps.Map(document.getElementById("map_canvas"), {
于 2013-11-13T09:13:04.310 回答