谷歌地图位置搜索问题

时间:2018-03-21 09:08:27

标签: javascript php google-maps mysqli

我正在研究核心PHP,我想基于位置的记录。

我想要精确搜索谷歌地图,如果我搜索(实现自动完成搜索)(BTM布局)意味着BTM布局谷歌地图应该怎么做这个请帮助任何人。

这是我的HTML表单:

     <form action="pglist.php" id="location_search" class="form-inline" method="POST" >
    <div class="form-group keyword">
            <input id="locationTextField" name= "list" type="text" size="50">   
            <button type="submit" name="filter"><img src="assets/images/search.png" alt="Rentozy" /></button>
 </div>
 </form>

这是我的数据库查询:

$locations=array();
      $query =  $conn->query('SELECT `pg_address` FROM `tbl_master_property`');
       while ($row = $query->fetch_assoc()) {
        $locations[] = $row['pg_address'];
      }
      $locations = json_encode($locations);

这是我的谷歌地图脚本代码:

<div class="map">
      <div id="map"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyBAM5Cs2VsrOBs8Idqy0t0o6vw4hEU0Lys">
 </script> 
 <script type="text/javascript">
  var delay = 100;
  var infowindow = new google.maps.InfoWindow();
  var latlng = new google.maps.LatLng(12.9716, 77.5946);
  var mapOptions = {
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var geocoder = new google.maps.Geocoder(); 
  var map = new google.maps.Map(document.getElementById("map"), mapOptions);
  var bounds = new google.maps.LatLngBounds();
  function geocodeAddress(address, next) {
    geocoder.geocode({address:address}, function (results,status)
      { 
         if (status == google.maps.GeocoderStatus.OK) {
          var p = results[0].geometry.location;
          var lat=p.lat();
          var lng=p.lng();
          createMarker(address,lat,lng);
        }
        else {
           if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            nextAddress--;
            delay++;
          } else {
                }   
        }
        next();
      }
    );
  }
 function createMarker(add,lat,lng) {
   var contentString = add;
   var marker = new google.maps.Marker({
     position: new google.maps.LatLng(lat,lng),
     animation: google.maps.Animation.DROP,
     draggable: true,
     map: map,
           });
    marker.addListener('click', toggleBounce);
    function toggleBounce() {
        if (marker.getAnimation() !== null) {
          marker.setAnimation(null);
        } else {
          marker.setAnimation(google.maps.Animation.BOUNCE);
        }
      }
  google.maps.event.addListener(marker, 'click', function() {
     infowindow.setContent(contentString); 
     infowindow.open(map,marker);
   });
   bounds.extend(marker.position);
 }
 var locations = <?= $locations ?>;
console.log(locations);
  var nextAddress = 0;
  function theNext() {
    if (nextAddress < locations.length) {
      setTimeout('geocodeAddress("'+locations[nextAddress]+'",theNext)', delay);
      nextAddress++;
    } else {
      map.fitBounds(bounds);
    }
  }
  theNext();

</script>

1 个答案:

答案 0 :(得分:0)

geocoder = new google.maps.Geocoder();

geocoder.geocode({'address': 'BTM layout'}, function(results, status) {
          if (status === 'OK') {
            Map.setCenter(results[0].geometry.location);
          } else {
           alert('Geocode was not successful for the following reason: ' + status);
          }
        });

您可以使用地理编码器请求该位置并处理例外情况。