Google Map未显示在Ajax进程中

时间:2013-08-24 11:46:05

标签: jquery ajax google-maps-api-3

使用此代码我在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>

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

0 个答案:

没有答案