将PHP / MySQL与带有自动填充搜索框的Google地图结合使用

时间:2013-04-18 08:14:11

标签: google-places-api

我正在使用https://developers.google.com/maps/articles/phpsqlajax_v3

    <!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>Consultants Map</title>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
    //<![CDATA[

    var customIcons = {
      restaurant: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(46.80, -100.78),
        zoom: 4,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      //autocomplete.bindTo('bounds', map);

      // Change this depending on the name of your PHP file
      downloadUrl("consultants_maps.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
                var html = "<b>" + name + "</b> <br/>"  + address;

          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
          });
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

    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" style="width: 1280px; height: 720px"></div>
  </body>
</html>

这也是php的代码:

<?php
require_once('wp-config.php');
require_once('wp-load.php');
require_once('wp-includes/wp-db.php');

    global $wpdb;
    $table_name     = $wpdb->prefix . "consultants_map";

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 

$query = "SELECT * FROM $table_name WHERE 1";
$result = mysql_query($query);

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $address = $row['kt_number'] . "<br />" . $row['address'];
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);   
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("address", $address);  
  $newnode->setAttribute("lat", $row['lat']);  
  $newnode->setAttribute("lng", $row['lng']);  
  //$newnode->setAttribute("type", $row['type']);
} 

echo $dom->saveXML();

?>

它对我来说很好

但我想在这里添加自动填充文本框功能 https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete

如何将这两个代码组合在一起?

请帮帮我。

谢谢

1 个答案:

答案 0 :(得分:0)

按照您在https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete链接的确切示例,您可以注意到您缺少的关键位:

  1. 在加载图书馆时places添加librarieshttps://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places
  2. 创建自动填充对象var autocomplete = new google.maps.places.Autocomplete(document.getElementById('searchTextField'));
  3. autocomplete.bindTo('bounds', map);然后绑定
  4. 照顾上述内容应该允许所有功能通过。

    您还应该添加一个事件监听器google.maps.event.addListener(autocomplete, 'place_changed', function() { ...,它会在您的位置发生变化时触发,autocomplete.getPlace()可以获取包含有关所选位置的大量信息的place对象。只需按照您找到的链接中的示例获取更多详细信息和示例。