Google地图无法加载

时间:2012-01-05 01:09:53

标签: php javascript html google-maps-api-3

我在测试页面上测试了这段代码,但现在想要在覆盖(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>

1 个答案:

答案 0 :(得分: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(如谷歌地图)。

接下来,您正在调用一个您根本不解释的PHP脚本require_once('../maps/google.php');。它是什么?你在哪里得到它?你有没有咨询过他们的教程或文档?

您只是向我们展示了问题的一部分。您需要返回Square 1并在向前移动时检查每一步。跳到最后(从代码的外观来看,这是你试图做的)意味着Square 1之间的一百步之一可能已经坏了,现在你必须回去寻找它