【问题标题】:Google Maps API v3 - Place Google Maps Circle as Lat/Lng from Search BoxGoogle Maps API v3 - 将 Google Maps Circle 作为搜索框中的 Lat/Lng
【发布时间】:2018-06-30 04:13:43
【问题描述】:

正如我的代码显示的那样,这很新。我的代码主要取自 Google Developers 页面,用于使用 Google Maps API v3。

我正在尝试使用 PHP 将一定数量的位置从 MySql 数据库加载到 Google 地图中。然后,我希望能够使用搜索框的结果 lat/lng 在地图上放置一个可编辑的圆圈,以便用户可以更改半径并移动圆圈并查看圆圈覆盖的位置。如果用户进行新的搜索,我打算从搜索框中删除圆圈并再次替换为新的 lat/lng。

到目前为止,我已经做到了:

  1. 使用 PHP 将位置从 MySql 加载到 Google 地图中
  2. 插入一个搜索框,可以平移到找到的地址
  3. 在设置的纬度/经度处输入一个可编辑的圆圈。这就是我遇到困难的地方,因为我希望动态放置可编辑的圆圈。

任何帮助将不胜感激。谢谢。

21.01.2018 19:35 GMT:以下代码的新示例取自 Google 地图开发者页面,因为发布的旧版本代码具有指向 MySQL 数据库的 PHP 链接,其他人无法加载。这是来自 Google Developers 页面 (https://developers.google.com/maps/documentation/javascript/examples/places-searchbox) 的搜索框示例代码,我添加了一些非常基本的代码来添加可编辑的圆圈。

同样的想法是,当用户搜索一个位置而不是在地图上放置一个标记时,会放置一个可编辑的圆圈。每次为可编辑圆圈搜索新位置时,都会从地图中清除。我会有一个输入框,用户可以在其中输入以米为单位的圆的半径。当半径被输入框时,圆的半径会改变以匹配它。

新代码:

 <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Places Searchbox</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #description {
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
      }

      #infowindow-content .title {
        font-weight: bold;
      }

      #infowindow-content {
        display: none;
      }

      #map #infowindow-content {
        display: inline;
      }

      .pac-card {
        margin: 10px 10px 0 0;
        border-radius: 2px 0 0 2px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        outline: none;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
        background-color: #fff;
        font-family: Roboto;
      }

      #pac-container {
        padding-bottom: 12px;
        margin-right: 12px;
      }

      .pac-controls {
        display: inline-block;
        padding: 5px 11px;
      }

      .pac-controls label {
        font-family: Roboto;
        font-size: 13px;
        font-weight: 300;
      }

      #pac-input {
        background-color: #fff;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        margin-left: 12px;
        padding: 0 11px 0 13px;
        text-overflow: ellipsis;
        width: 400px;
      }

      #pac-input:focus {
        border-color: #4d90fe;
      }

      #title {
        color: #fff;
        background-color: #4d90fe;
        font-size: 25px;
        font-weight: 500;
        padding: 6px 12px;
      }
      #target {
        width: 345px;
      }
    </style>
  </head>
  <body>
    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
    <div id="map"></div>
    <script>
      // This example adds a search box to a map, using the Google Place Autocomplete
      // feature. People can enter geographical searches. The search box will return a
      // pick list containing a mix of places and predicted search terms.

      // This example requires the Places library. Include the libraries=places
      // parameter when you first load the API. For example:
      // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

      function initAutocomplete() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -33.8688, lng: 151.2195},
          zoom: 13,
          mapTypeId: 'roadmap'
        });

        // 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.

        //ADDING CIRCLE HERE - 21.01.20178 19:25PM GMT

        var circle = new google.maps.Circle ({
        map: map,
        center: new google.maps.LatLng (-33.856717, 151.215288),
        radius: 1000,
        strokeColor: "#00ff00",
        fillcolor: "red",
        editable: true
  });

        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)
            };

            // Create a marker for each place.
            markers.push(new google.maps.Marker({
              map: map,
              icon: icon,
              title: place.name,
              position: place.geometry.location
            }));

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

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

【问题讨论】:

    标签: javascript google-maps-api-3 geometry search-box


    【解决方案1】:

    要将圆圈移动到SearchBox 中的第一个结果,请将圆圈的中心设置为结果的位置。将此添加到place_changed 事件处理中:

    if (index == 0) {
      circle.setCenter(place.geometry.location);
    }
    

    proof of concept fiddle

    代码 sn-p:

    function initAutocomplete() {
      var map = new google.maps.Map(document.getElementById('map'), {
        center: {
          lat: -33.8688,
          lng: 151.2195
        },
        zoom: 13,
        mapTypeId: 'roadmap'
      });
    
      // 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 = [];
    
      var circle = new google.maps.Circle({
        map: map,
        center: new google.maps.LatLng(-33.856717, 151.215288),
        radius: 1000,
        strokeColor: "#00ff00",
        fillcolor: "red",
        editable: true
      });
    
      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, index) {
          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)
          };
    
          // Create a marker for each place.
          markers.push(new google.maps.Marker({
            map: map,
            icon: icon,
            title: place.name,
            position: place.geometry.location
          }));
          
          // center the circle on the first result
          if (index == 0) {
            circle.setCenter(place.geometry.location);
          }
          if (place.geometry.viewport) {
            // Only geocodes have viewport.
            bounds.union(place.geometry.viewport);
          } else {
            bounds.extend(place.geometry.location);
          }
        });
        map.fitBounds(bounds);
      });
    }
    google.maps.event.addDomListener(window, "load", initAutocomplete);
    /* Always set the map height explicitly to define the size of the div
           * element that contains the map. */
    
    #map {
      height: 100%;
    }
    
    
    /* Optional: Makes the sample page fill the window. */
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    
    #description {
      font-family: Roboto;
      font-size: 15px;
      font-weight: 300;
    }
    
    #infowindow-content .title {
      font-weight: bold;
    }
    
    #infowindow-content {
      display: none;
    }
    
    #map #infowindow-content {
      display: inline;
    }
    
    .pac-card {
      margin: 10px 10px 0 0;
      border-radius: 2px 0 0 2px;
      box-sizing: border-box;
      -moz-box-sizing: border-box;
      outline: none;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
      background-color: #fff;
      font-family: Roboto;
    }
    
    #pac-container {
      padding-bottom: 12px;
      margin-right: 12px;
    }
    
    .pac-controls {
      display: inline-block;
      padding: 5px 11px;
    }
    
    .pac-controls label {
      font-family: Roboto;
      font-size: 13px;
      font-weight: 300;
    }
    
    #pac-input {
      background-color: #fff;
      font-family: Roboto;
      font-size: 15px;
      font-weight: 300;
      margin-left: 12px;
      padding: 0 11px 0 13px;
      text-overflow: ellipsis;
      width: 400px;
    }
    
    #pac-input:focus {
      border-color: #4d90fe;
    }
    
    #title {
      color: #fff;
      background-color: #4d90fe;
      font-size: 25px;
      font-weight: 500;
      padding: 6px 12px;
    }
    
    #target {
      width: 345px;
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
    <div id="map"></div>

    【讨论】:

    • 这真的很棒。谢谢你。谢谢你这么快回复我。祝你有美好的一天。我已将您的答案标记为已成功回答该问题。还有什么我可以做的吗?我无法将其标记为有用,因为我是新手...
    最近更新 更多