谷歌地图未加载

Hirantha

在ajax成功时,不会加载谷歌地图。数据库表每20秒更新一次,而ajax方法每30秒读取一次。ajax方法正常工作。它显示了控制台上的响应。map 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);
?>
gmfm

您是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中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章