Javascript 谷歌地图v2到v3代码

Javascript 谷歌地图v2到v3代码,javascript,php,google-maps,Javascript,Php,Google Maps,我正在做一个从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('$_ADDR

我正在做一个从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;
}
?>

有什么想法吗?
感谢您查看javascript的基本问题

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

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

echo $map = str_replace('$_ADDRESS', "'MYADDRESS'", $map );
function initialize( address ) 
map = new google.maps.Map(document.getElementById("map_canvas"), {
这样,javascript中包含地址的字符串将被正确引用

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

function initialize() 
var map = new google.maps.Map(document.getElementById("map_canvas"), {
变成:

echo $map = str_replace('$_ADDRESS', "'MYADDRESS'", $map );
function initialize( address ) 
map = new google.maps.Map(document.getElementById("map_canvas"), {
最后,在“初始化”中为“映射”赋值时,不要重新声明“映射”,否则您引用的是仅存在于该函数中的不同变量:

function initialize() 
var map = new google.maps.Map(document.getElementById("map_canvas"), {
变成:

echo $map = str_replace('$_ADDRESS', "'MYADDRESS'", $map );
function initialize( address ) 
map = new google.maps.Map(document.getElementById("map_canvas"), {

我将这些答案分成两部分,第一部分是javascript的基本原理,第二部分是使用GoogleMapsAPI

由于我从未使用过maps 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;
}
?>


话虽如此,我还是要质疑str_replace直接进入javascript——你能相信数据的来源吗?如果没有,那么在将字符串放入javascript之前,您应该查看如何清理该字符串,否则您可能会允许人们将代码注入您的站点。

您收到了什么错误?那么预期的行为是什么?@karl johab:空白页没有错误当你说空白页时,如果你“查看来源”,你会看到什么。有人会认为脚本就在那里。。@karl johab:在源代码视图中,我没有得到任何错误。它显示了用php文件编写的js、html代码,刚刚发现您将MYADDRESS传递到初始化函数中,但该函数没有任何参数。然后将地址传递到showAddress,但该变量从未设置。现在我更改了该行:函数初始化(address),并在initialize和showAddress函数中获取地址值,但再次不更改地址。我是否在showAddress函数中使用了错误的google maps方法?我必须承认,我从未使用过maps API,所以我必须四处看看,看看需要做什么。但关于基本javascript问题的最后一点是,在initialize中引用的“map”变量与在showAddress中引用的变量不同。也就是说,你用“var”重新声明了它。我编辑了我的答案以反映这一点