谷歌地图v2到v3代码

时间:2013-11-13 08:49:45

标签: javascript php 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;
}
?>

任何想法? 感谢

2 个答案:

答案 0 :(得分: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;
}
?>

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

答案 1 :(得分:0)

查看您的基本JavaScript问题......

尝试在您要替换为javascript

的字符串周围添加引号
echo $map = str_replace('$_ADDRESS', 'MYADDRESS', $map );

变为:

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

这样就可以正确引用包含javascript中地址的字符串。

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

function initialize() 

变为:

function initialize( address ) 

最后,在“初始化”中为其赋值时,不要重新声明“map”,否则您将引用仅存在于该函数中的另一个变量:

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

变为:

map = new google.maps.Map(document.getElementById("map_canvas"), {
相关问题