簡體   English   中英

Google Map v2到v3的代碼

[英]Google map v2 to v3 code

我正在做一個從Google Maps V2到V3的遷移項目,並編寫了下面的代碼,但出現錯誤,無法解決問題。

我使用的Google地圖方法錯誤嗎?

該代碼有什么問題?

<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的基礎知識,然后再處理使用Google Maps API的問題。

由於我從未使用過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之前,您應該先查看如何清理該字符串,否則您可能會允許人們將代碼注入您的網站。

查看您遇到的基本JavaScript問題...

嘗試在要替換為javascript的字符串周圍添加引號

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

變為:

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

這樣,包含javascript中地址的字符串將被正確引用。

另外,請確保您可以在初始化函數中收到地址:

function initialize() 

變為:

function initialize( address ) 

最后,在“初始化”中為其分配值時,請勿重新聲明“映射”,否則,您引用的是僅存在於該函數中的另一個變量:

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

變為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM