簡體   English   中英

谷歌地圖未加載

[英]Google map is not load

在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);
?>

您正在 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 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM