2012-01-05 163 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