2012-01-05 118 views
-1

我在測試頁面上測試了這段代碼,但現在想在覆蓋層(Lightview)調用的頁面上實現它。地圖不顯示,我不知道爲什麼。覆蓋圖將空間大小映射到地圖的大小,但沒有顯示。下面是代碼:谷歌地圖無法加載

<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> </script> 
<?php 
require_once('../maps/google.php'); 

if(isset($_POST['submit'])) 
{ 
$zipcode = $_REQUEST['zipcode']; 
$lookupPerformed = false; 

if (strlen($zipcode) > 0) { 
    $geocoder = new Geocoder('mykey'); 


    try { 
     $placemarks = $geocoder->lookup($zipcode); 
    } 
    catch (Exception $ex) { 
     echo $ex->getMessage(); 
     exit; 
    } 

    $lookupPerformed = true; 
} 
foreach ($placemarks as $placemark) { 

$lat = $placemark->getPoint()->getLatitude(); 
$long = $placemark->getPoint()->getLongitude(); 
} 

?> 
<script type="text/javascript"> 
//<![CDATA[ 

var customIcons = { 
    A Company: { 
    icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png', 
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' 
    }, 
}; 

function load() { 
    var map = new google.maps.Map(document.getElementById("map"), { 
    center: new google.maps.LatLng(<?php echo $lat;?>, <?php echo $long;?>), 
    zoom: 10, 
    mapTypeId: 'roadmap' 
    }); 
    var infoWindow = new google.maps.InfoWindow; 

    // Change this depending on the name of your PHP file 
    downloadUrl("../testxml.php?zipcode=<?php echo $zipcode;?>", function(data) { 
    var xml = data.responseXML; 
    var markers = xml.documentElement.getElementsByTagName("marker"); 
    for (var i = 0; i < markers.length; i++) { 
     var name = markers[i].getAttribute("name"); 
     var address = markers[i].getAttribute("address"); 
     var type = markers[i].getAttribute("type"); 
     var point = new google.maps.LatLng(
      parseFloat(markers[i].getAttribute("lat")), 
      parseFloat(markers[i].getAttribute("lng"))); 
     var html = "<b>" + name + "</b> <br/>" + address; 
     var icon = customIcons[type] || {}; 
     var marker = new google.maps.Marker({ 
     map: map, 
     position: point, 
     icon: icon.icon, 
     shadow: icon.shadow 
     }); 
     bindInfoWindow(marker, map, infoWindow, html); 
    } 
    }); 
} 

function bindInfoWindow(marker, map, infoWindow, html) { 
    google.maps.event.addListener(marker, 'click', function() { 
    infoWindow.setContent(html); 
    infoWindow.open(map, marker); 
    }); 
} 

function downloadUrl(url, callback) { 
    var request = window.ActiveXObject ? 
     new ActiveXObject('Microsoft.XMLHTTP') : 
     new XMLHttpRequest; 

    request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
     request.onreadystatechange = doNothing; 
     callback(request, request.status); 
    } 
    }; 

    request.open('GET', url, true); 
    request.send(null); 
} 

function doNothing() {} 

//]]> 

</script> 
///// other PHP code 

</head> 

     <form method="POST" id="ajaxForm" onsubmit="submitAjaxFormDemonstration()"> 
     <input type="text" size="10" maxlength="10" name="zipcode" tabindex="1" value=" <?php echo $_POST['zipcode'];?>" /> 
     <input type="submit" id="submit" value="Search" name="submit" tabindex="2" /> 
     </form> 
<body onload="load()"> 
<div id="map" style="width: 500px; height: 300px"></div> 
</body> 

</html> 

回答

0

你有事情出錯這裏一堆,而且有些事情,我甚至不能看到告訴你他們是否是對還是錯?

在底部張貼代碼,您有:

</head> 

     <form method="POST" id="ajaxForm" onsubmit="submitAjaxFormDemonstration()"> 
     <input type="text" size="10" maxlength="10" name="zipcode" tabindex="1" value=" <?php echo $_POST['zipcode'];?>" /> 
     <input type="submit" id="submit" value="Search" name="submit" tabindex="2" /> 
     </form> 
<body onload="load()"> 
<div id="map" style="width: 500px; height: 300px"></div> 
</body> 

</html> 

你有一個<form>...</form>元素聲明<body>...</body>標籤 - 這不是有效的HTML。

</head> 

<body onload="load()"> 

<form method="POST" id="ajaxForm" onsubmit="submitAjaxFormDemonstration()"> 
    <input type="text" size="10" maxlength="10" name="zipcode" tabindex="1" value="<?php echo $_POST['zipcode'];?>" /> 
    <input type="submit" id="submit" value="Search" name="submit" tabindex="2" /> 
</form> 

<div id="map" style="width: 500px; height: 300px"></div> 

</body> 
</html> 

修復了......但您應該使用類似W3C Validator的東西來檢查您的HTML代碼。錯誤的HTML幾乎總是會打破Javascript(如Google地圖)。

接下來,您正在調用一個PHP腳本,您根本沒有解釋它require_once('../maps/google.php');。它是什麼?你在哪裏得到它?你有沒有諮詢他們的教程或文檔?

您只是在這裏向我們展示部分問題。你需要回到方形1,並在你前進時檢查每一步。跳到最後(從代碼的外觀來看,就是你試圖做的),意味着Square 1和那一百步之間可能有一個破碎,現在你必須回去尋找它。

+0

感謝您的答案,但這並不能解決任何問題。 ../maps/google.php是地理編碼器。我回合拉長,兩者都在工作。我知道表單位於body標籤之外,但對沒有顯示的地圖沒有影響。有人與谷歌地圖或原型經驗的答覆? – savagenoob 2012-01-05 04:57:03

+0

我確實有Google地圖體驗,謝謝。您是否能夠顯示簡單/標準的地圖,而不使用Geocoder?如果是這樣,Geocoder(無論你從哪裏得到)都是問題所在。如果您無法以任何方式查看地圖,那麼這是您的HTML問題。你不能在這裏轉儲你的代碼,只能顯示它的一部分,並期望人們發現問題。 – 2012-01-05 05:15:17