Google地图地点搜索框不起作用

问题描述 投票:0回答:3

在我的laravel项目中,我将诊所的位置显示为标记(从php数据库中获取经度和纬度,并将其显示为标记。我的要求是,我还想在Google地图顶部添加一个搜索框,并且如果我搜索该特定诊所的任何地点,则必须将其显示为标记。

我对laravel和Google地图是陌生的,我不知道为什么它不起作用。请帮我。我已经推荐了Google map develeoper docs Google map places search box

但是它不起作用,到目前为止,位置都没有显示为标记,但是搜索框不起作用。

以下是我的代码

<!DOCTYPE html>
<html>
  <head>
    <style>
       /* Set the size of the div element that contains the map */
      #map {
        height: 400px;  /* The height is 400 pixels */
        width: 100%;  /* The width is the width of the web page */
       }
    </style>
  </head>
  <body>
    <h3>My Google Maps Demo</h3>
    <!--The div element for the map -->
    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
    <div id="map"></div>


    <script>
var services =<?php echo $locservices;?>;
console.log(services);


function initMap() {
    var locations = <?php echo $locservices;?>;

    var j;
    for (j = 0; j < locations.length; j++) { 
    var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 8,
    center: {lat: parseFloat(locations[j]['locationLat']), lng:parseFloat(locations[j]['locationLong'])}
  });
    setMarkers(map);
  }
}
function setMarkers(map) {
var locations = <?php echo $locservices;?>;
for (var i = 0; i < locations.length; i++) {


   var marker = new google.maps.Marker({
                map: map,
                position: {lat: parseFloat(locations[i]['locationLat']), lng:parseFloat(locations[i]['locationLong'])},
                map: map,
                title: locations[i]['locationName']
               });
    var infowindow = new google.maps.InfoWindow()

    var content = "test" 

    google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){ 
    return function() {
        infowindow.setContent(content);
        infowindow.open(map,marker);
    };
})(marker, content,infowindow));  

  }

}

 </script>

    <script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap">
 </script>
  </body>
</html>
php laravel google-maps google-maps-markers google-map-place
3个回答
0
投票

尝试增加模态的z索引。我遇到了类似的问题,并且z-index的增加对我有所帮助

.pac-container {
    background-color: #FFF;
    z-index: 20;
    position: fixed;
    display: inline-block;
    float: left;
}
.modal{
    z-index: 20;   
}
.modal-backdrop{
    z-index: 10;        
}​

尽管,此答案是从另一个Stack Overflow Answer中检索的。我发布了有关访问此问题的任何人的参考和指导。


0
投票

请按照以下方式更正代码

<!DOCTYPE html>
<html>
  <head>
    <style>
       /* Set the size of the div element that contains the map */
      #map {
        height: 400px;  /* The height is 400 pixels */
        width: 100%;  /* The width is the width of the web page */
       }
    </style>
  </head>
  <body>
    <h3>My Google Maps Demo</h3>
    <!--The div element for the map -->
    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
    <div id="map"></div>
    <script>

      function initAutocomplete() {
        var locations = <?php echo $locservices;?>;
        console.log(locations);
        var j;
        for (j = 0; j < locations.length; j++){
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: parseFloat(locations[j]['locationLat']), lng:parseFloat(locations[j]['locationLong'])},
          zoom: 8,
          mapTypeId: 'roadmap'
        });
        setMarkers(map);
      }

        // Create the search box and link it to the UI element.
        var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input);
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function() {
          searchBox.setBounds(map.getBounds());
        });

        var markers = [];
        // Listen for the event fired when the user selects a prediction and retrieve
        // more details for that place.
        searchBox.addListener('places_changed', function() {
          var places = searchBox.getPlaces();

          if (places.length == 0) {
            return;
          }

          // Clear out the old markers.
          markers.forEach(function(marker) {
            marker.setMap(null);
          });
          markers = [];

          // For each place, get the icon, name and location.
          var bounds = new google.maps.LatLngBounds();
          places.forEach(function(place) {
            if (!place.geometry) {
              console.log("Returned place contains no geometry");
              return;
            }
            var icon = {
              url: place.icon,
              size: new google.maps.Size(71, 71),
              origin: new google.maps.Point(0, 0),
              anchor: new google.maps.Point(17, 34),
              scaledSize: new google.maps.Size(25, 25)
            };

            if (place.geometry.viewport) {
              // Only geocodes have viewport.
              bounds.union(place.geometry.viewport);
            } else {
              bounds.extend(place.geometry.location);
            }
          });
          map.fitBounds(bounds);
        });
      }

      function setMarkers(map) {
var locations = <?php echo $locservices;?>;


//console.log(services);

for (var i = 0; i < locations.length; i++) {


   var marker = new google.maps.Marker({
                map: map,
                position: {lat: parseFloat(locations[i]['locationLat']), lng:parseFloat(locations[i]['locationLong'])},
                map: map,
                title: locations[i]['locationName']
               });
    var infowindow = new google.maps.InfoWindow()

    var content = "test" 

    google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){ 
    return function() {
        infowindow.setContent(content);
        infowindow.open(map,marker);
    };
})(marker, content,infowindow));  

  }

}

    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAVzWibWlEc43MAKZk2N1PG6suW50uTnI4&libraries=places&callback=initAutocomplete"
         async defer></script>
  </body>
</html>

0
投票

尝试此功能

<script type='text/javascript'>
  function initialize() {
    var input = document.getElementById('search_address');

    var autocomplete = new google.maps.places.Autocomplete(input);
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

<script type='text/javascript'>
  function initialize() {
    var input = document.getElementById('listing_address');

    var autocomplete = new google.maps.places.Autocomplete(input);
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>
© www.soinside.com 2019 - 2024. All rights reserved.