使用php sql从数据库谷歌地图标记

时间:2015-06-25 13:08:58

标签: javascript php sql google-maps

我有这个代码在谷歌地图上填充数据库中的标记,但它不起作用,xml文件完全正常工作,我提到https://developers.google.com/maps/articles/phpsqlajax_v3,但没有用。

markers.php代码

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jainmunilocator";
$connection=mysqli_connect($servername, $username, $password, $dbname);;
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT name,id,lat,lng FROM muni_location,munishri WHERE mid=id";
header('Content-Type: application/xml');
$result=mysqli_query($connection,$sql);
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
if (mysqli_num_rows($result) > 0)
  {
while($row = mysqli_fetch_assoc($result)){
if(isset($row['id'])){ $id = $row['id']; } 
 if(isset($row['name'])){ $name = $row['name']; } 
 if(isset($row['lat'])){ $lat = $row['lat']; } 
 if(isset($row['lng'])){ $lng = $row['lng']; } 
$node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("id",$id);
  $newnode->setAttribute("name", $name);
  $newnode->setAttribute("lat", $lat);
  $newnode->setAttribute("lng", $lng);
  }
  }
 echo $dom->saveXML();

?>

* GOOGLE MAP MARKER CODE **

    <!DOCTYPE html >
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <title>PHP/MySQL & Google Maps Example</title>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
        <script type="text/javascript">
        //<![CDATA[


        function load() {
          var map = new google.maps.Map(document.getElementById("map"), {
            center: new google.maps.LatLng(22.6145,77.3418),
            zoom: 7,
            mapTypeId: 'roadmap'
          });
          var infoWindow = new google.maps.InfoWindow;
          var gm_marker = new Array();     

          // Change this depending on the name of your PHP file
          downloadUrl("markers.php", function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
              var id = markers[i].getAttribute("id");
              var name = markers[i].getAttribute("name");
              var point = new google.maps.LatLng(
                  parseFloat(markers[i].getAttribute("lat")),
                  parseFloat(markers[i].getAttribute("lng")));
              //var html = "<b>" + name + "</b> <br/>" + id;
              var html = name;
              var icon = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                title: name
              });
              addMarker(point,name);
              bindInfoWindow(marker, map, infoWindow, html);

            }
          });
        }

        //stackoverflow
        function addMarker(latLng, contentstr) {
      var marker = new google.maps.Marker({
        position: latLng,
        map: map
      });
      google.maps.event.addListener(marker, 'click', function() {   
          infowindow = new google.maps.InfoWindow({
              content: contentstr
          });  

       });
      gm_markers.push(marker);

    }
    setAllmap(map);
    function setAllMap(map) {
  for (var i = 0; i < gm_markers.length; i++) {
    gm_markers[i].setMap(map);
  }
  function showMarkers() { setAllMap(map);}


        function bindInfoWindow(marker, map, infoWindow, html) {
          google.maps.event.addListener(marker, 'click', function( ){
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
          });
        }

        function downloadUrl(url, callback) {
          var request = window.ActiveXObject ?
              new ActiveXObject('Microsoft.XMLHTTP') :
              new XMLHttpRequest;

          request.onreadystatechange = function() {
            if (request.readyState == 4) {
              request.onreadystatechange = doNothing;
              callback(request, request.status);
            }
          };

          request.open('GET', url, true);
          request.send(null);
        }

        function doNothing() {}

        //]]>

      </script>

      </head>

      <body onload="load()">
      <div id="map-canvas-show" style="width: 100%; height: 300px;"></div> 
      </body>

    </html>

1 个答案:

答案 0 :(得分:1)

我认为有两个要素是必不可少的,我在你的代码中看不到。这是我的来源,我在这里包括他们来展示概念。你可以在for循环中实现这种类型的东西,或者分解成函数。我更喜欢功能,所以我这样做了。

首先,我会确认你的标记数组不是长度为0 - 这是一个简单的健全性检查 - 确保你有标记要加载。

构建Google地图标记堆栈

虽然有标记......

  1. 使用新

  2. 创建标记
  3. 使用新的

  4. 创建侦听器
  5. 将标记作为标记对象数组推入Google的标记对象

  6. // Add a marker to the map and push to the array.
    function addMarker(latLng, contentstr) {
      var marker = new google.maps.Marker({
        position: latLng,
        map: map
      });
      google.maps.event.addListener(marker, 'click', function() {   
          infowindow = new google.maps.InfoWindow({
              content: contentstr
          });  
    
       });
      gm_markers.push(marker);
    
    }
    

    显示标记将标记分配给地图

    将数组中的每个标记与地图画布相关联。

    // Sets the map on all markers in the array.
    function setAllMap(map) {
      for (var i = 0; i < gm_markers.length; i++) {
        gm_markers[i].setMap(map);
      }
    

    清除Google地图标记(关闭)

    强化这个概念。要清除标记,请从地图对象中拔出地图标记对象数组。

    // Removes the markers from the map, but keeps them in the array.
    function clearMarkers() {
      setAllMap(null);
    
    }
    

    显示已加载的标记(打开)

    // Shows any markers currently in the array.
    function showMarkers() {
      setAllMap(map);
    
    }
    

    在视图中

    显示带有标记的地图

    <div id="map-canvas-show" style="width: 100%; height: 300px;"></div> 
    

    调试步骤(已添加)

    1. 我建议首先确认有效坐标正在加载到php 中的标记变量中。事实上,我会先用一个标记进行测试。输入调试语句以显示lat和lng。这很重要。从php确认有效的标记数据到你的js。

    2. 然后,您需要确认这些标记是否在Google地图的可见范围内。那里的标记是不可见的?为了提供帮助,我建议您将谷歌地图缩小 - 缩放它,以便在世界各地看到一个标记。