0

使用此代码,我在 index.php 页面中显示谷歌地图:

<!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

<!-- Include Google Maps JS -->
<script src="http://maps.googleapis.com/maps/api/js?v=3&libraries=geometry&sensor=true" type="text/javascript"></script>

$indirizzo = "corso Buones Aires";
$comune = "Milano";
$map = new simpleGMapAPI();
$geo = new simpleGMapGeocoder();

$map->setWidth(332);
$map->setHeight(250);
$map->setBackgroundColor('#d0d0d0');
$map->setMapDraggable(true);
$map->setDoubleclickZoom(true);
$map->setScrollwheelZoom(true);

$map->showDefaultUI(true);
$map->showMapTypeControl(true, 'DEFAULT');
$map->showNavigationControl(true, 'DEFAULT');
$map->showScaleControl(true);
$map->showStreetViewControl(false);

$map->setZoomLevel(9);
$map->setInfoWindowBehaviour('SINGLE_CLOSE_ON_MAPCLICK');
$map->setInfoWindowTrigger('CLICK');

$map->addMarkerByAddress($indirizzo . " " . $comune, $indirizzo, $indirizzo, "http://google-maps-icons.googlecode.com/files/villa.png");

echo $map->showMap(false);

这段代码很好用,但如果我尝试在 ajax 进程中获取谷歌地图,我无法显示它。

换句话说,如果我将上面的代码放在 map.php 页面中

<?php
  require("../inc/config.php");


  echo "           <!--[if IE]>\n";
  echo "          <script src=\"http://html5shim.googlecode.com/svn/trunk/html5.js\"></script>\n";
  echo "          <![endif]-->\n";
  echo "          <!-- Include Google Maps JS -->\n";
  echo "        <script src=\"http://maps.googleapis.com/maps/api/js?v=3&libraries=geometry&sensor=true\" type=\"text/javascript\"></script>";



  if($_POST["comune"])
   {

     $indirizzo = "corso Buones Aires";
     $comune = $_POST["comune"];

     $map = new simpleGMapAPI();
     $geo = new simpleGMapGeocoder();

     $map->setWidth(332);
     $map->setHeight(250);
     $map->setBackgroundColor('#d0d0d0');
     $map->setMapDraggable(true);
     $map->setDoubleclickZoom(true);
     $map->setScrollwheelZoom(true);

     $map->showDefaultUI(true);
     $map->showMapTypeControl(true, 'DEFAULT');
     $map->showNavigationControl(true, 'DEFAULT');
     $map->showScaleControl(true);
     $map->showStreetViewControl(false);

     $map->setZoomLevel(9); 
     $map->setInfoWindowBehaviour('SINGLE_CLOSE_ON_MAPCLICK');
     $map->setInfoWindowTrigger('CLICK');

     $map->addMarkerByAddress($indirizzo . " " . $comune, $indirizzo, $indirizzo, "http://google-maps-icons.googlecode.com/files/villa.png");


     echo $map->showMap(false);

     }

      ?>

并使用 jquery 调用此页面:

$(document).ready(function(){
 $("#spanComune").change(function(){
   var $body = $("body"),
   comune=$("#spanComune option:selected").text();
   $body.addClass("loading");
 $.ajax({
  type:"POST",
  url:"ajax/map.php",
  data: "comune="+comune,
  success:function(data){
  $("#spanMappa").empty().html(data);
  $body.removeClass("loading");
  }
 });
 });
});

在 index.php 中:

 <div id="spanMappa"></div>

谷歌地图不显示。很奇怪的问题!我该如何解决这个问题?

4

0 回答 0