2016-04-04 190 views
2

在ajax成功,谷歌地圖不加載。數據庫表每20秒更新一次,而ajax方法每30秒讀一次。 ajax方法正常工作。它顯示控制檯上的響應。地圖div已加載,但地圖未加載。請幫忙。谷歌地圖不加載

Ajax調用頁面。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script> 
<script src="http://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=geometry"></script> 

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

<script> 
var response; 
var ajax_call = function() { 
    $.ajax({          
     url: 'ajaxRequest.php',       
     data: "",       
     dataType: 'json',      
     success: function(data)   
     { 
     response = data; 
     console.log(response); 
     initialize(); 
     } 
    }); 
}; 

var interval = 5000; 
setInterval(ajax_call, interval); 

var myCenter=new google.maps.LatLng(41.669578,-0.907495); 
    var marker; 
    var map; 
    var mapProp; 

    function initialize() 
    { 
     mapProp = { 
      center:myCenter, 
      zoom:15, 
      mapTypeId:google.maps.MapTypeId.ROADMAP 
      }; 
     setInterval('mark()',5000); 
    } 

    function mark() 
    { 
     map=new google.maps.Map(document.getElementById("map"),mapProp); 
     marker=new google.maps.Marker({ 
      position:new google.maps.LatLng(response) 
      }); 
      marker.setMap(map); 
      map.setCenter(new google.maps.LatLng(response)); 
      marker.setAnimation(google.maps.Animation.BOUNCE); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

這是ajaxRequest.php頁

<?php 
include "db_connect.php"; 
$sql="SELECT * FROM temperature_details ORDER BY id DESC LIMIT 1 "; 
$result=mysql_query($sql); 

$firstrow = mysql_fetch_assoc($result); 
$outPut = $firstrow['latitude'].','. $firstrow['longitude']; 
echo json_encode($outPut); 
?> 
+0

請提供一個[最小,完整,測試和可讀的示例](http://stackoverflow.com/help/mcve),它演示了這個問題(我看到一個帶有發佈代碼的地圖,_your_地圖是否有尺寸?是否有大小的父元素?) – geocodezip

+0

是的地圖有一個大小 – Hirantha

回答

1

你是JSON編碼字符串,而不是一個對象或數組。 LatLng不知道如何處理你的字符串。

嘗試:

<?php 
include "db_connect.php"; 
$sql="SELECT * FROM temperature_details ORDER BY id DESC LIMIT 1 "; 
$result=mysql_query($sql); 
$firstrow = mysql_fetch_assoc($result); 
$outPut = array($firstrow['latitude'], $firstrow['longitude']); 
echo json_encode($outPut); 
?> 

爲你的PHP,

google.maps.LatLng(response[0], response[1]) 

代替:

google.maps.LatLng(response) 
在JavaScript