从地图上的数据库和绘图标记获取gps Lat,Long值?

时间:2014-05-06 06:58:22

标签: javascript php google-maps google-maps-api-3 gps

我正在研究跟踪系统。 我从数据库中获取gps lat long值,然后在地图上显示标记, 并在30秒后刷新页面以检查服务器上是否更新b值。 我的代码如下。 可以映射显示页面。

  <!DOCTYPE html />
   <html>
  <head>
  <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript">                    </script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> </script>
    <title>GoogleMaps</title>
<script>

function initialize(lang,lat) {
  var myLatlng = new google.maps.LatLng(lang,lat);
 var myOptions = {
        zoom: 8,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var contentString = 'Location NAme';
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'Location NAme'
    });
}


function show_map(){
    $.ajax({
        type: "POST",
            url: "some.php"
        })
        .done(function( msg ) {
            alert(msg);
            var obj = jQuery.parseJSON(msg);

            initialize(obj.lang,obj.lat);

    });

}

setInterval(show_map,30000);
</script>

</head>
<body  onLoad="show_map()">

    <div id="map_canvas" style="width: 100%; height: 100%;">

</body>
</html>

我的PHP页面是:

   <?php
          mysql_connect("localhost","root","")or die("error in connection");
          mysql_select_db("gpsvalues");
          $result = mysql_query("select * from gps");
         $row = mysql_fetch_assoc($result);
         $arr=array();
         $arr = $row;
         echo json_encode($arr);
    ?>

但它没有用。

2 个答案:

答案 0 :(得分:0)

对于地图中的show multiple,您必须为lat long创建数组。

 var locations;
         locations=[
          [lat1,long1],
          [lat2,long2],
          [lat3,long3],
          [lat4,long4],
         ];

所以你的代码应该像

一样改变
<script>
         var locations;
         locations=[
          [lat1,long1],
          [lat2,long2],
          [lat3,long3],
          [lat4,long4],
         ];
    var map = new google.maps.Map(document.getElementById('maparea'), {
      zoom: 14,
      center: new google.maps.LatLng(6.778659644259302, 79.99540328979492),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][0], locations[i][1]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
</script>

答案 1 :(得分:0)

您可以通过PHP为gps中的每一行调用添加标记功能..

 <script>
    <?php
          mysql_connect("localhost","root","")or die("error in connection");
          mysql_select_db("gpsvalues");
          $result = mysql_query("select * from gps");
          while($row = mysql_fetch_assoc($result))
          {
              echo "addmarker($row[lat], $row[lng]);"; //whatever way you have stored in your database
          }
    ?>


          function addmarker(lat, lng) {
              var myLatlng = new google.maps.LatLng(lat,lng);
              var marker = new google.maps.Marker({
              position: myLatlng,
              map: map,
              title: 'Location NAme'
          });
          marker.setMap(map);
          }
    </script>