谷歌地图 - 获取infoWindow内容

时间:2016-11-23 17:39:29

标签: javascript jquery html google-maps

<!DOCTYPE html>
<html>
  <head>
    <title>PlaceID finder</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <script src="~/Scripts/jquery-3.1.0.min.js"></script>
    <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;
      }
      .controls {
        background-color: #fff;
        border-radius: 2px;
        border: 1px solid transparent;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
        box-sizing: border-box;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        height: 29px;
        margin-left: 17px;
        margin-top: 10px;
        outline: none;
        padding: 0 11px 0 13px;
        text-overflow: ellipsis;
        width: 400px;
      }

      .controls:focus {
        border-color: #4d90fe;
      }
    </style>
  </head>
  <body>
    <input id="pac-input" class="controls" type="text"
        placeholder="Enter a location">
    <div id="map"></div>
<input style="margin-left:5px" type="button" class="btn btn-primary" value="Add Store Point" id="addNewStore" />

    <script>
      // This sample uses the Place Autocomplete widget to allow the user to search
      // for and select a place. The sample then displays an info window containing
      // the place ID and other information about the place that the user has
      // selected.

      // 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 initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -33.8688, lng: 151.2195},
          zoom: 13
        });

        var input = document.getElementById('pac-input');

        var autocomplete = new google.maps.places.Autocomplete(input);
        autocomplete.bindTo('bounds', map);

        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

        var infowindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({
          map: map
        });
        marker.addListener('click', function() {
          infowindow.open(map, marker);
        });

        autocomplete.addListener('place_changed', function() {
          infowindow.close();
          var place = autocomplete.getPlace();
          if (!place.geometry) {
            return;
          }

          if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
          } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);
          }

          // Set the position of the marker using the place ID and location.
          marker.setPlace({
            placeId: place.place_id,
            location: place.geometry.location
          });
          marker.setVisible(true);

          infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
              'Place ID: ' + place.place_id + '<br>' +
              place.formatted_address);
          infowindow.open(map, marker);
        });
      }

$("#addNewStore").on("click", function () {
            //i need these variables
            //...=place.name;
            //...=place.place_id;
            //...=place.formatted_address;

           /$.ajax({
                url: '/Store/Add',
                type: 'POST',
                data: {
                    PlaceName:place.name,PlaceID:place.place_id, Address:place.formatted_address
                },
                success: function (results) {
                    alert("Successfully added new store");
                }, error: function (data) {
                    alert("failed to add store");
                }
            });
        });
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"
        async defer></script>
  </body>
</html>

该示例来自谷歌地图文档,我发现它对我非常有用。 我的问题是我无法在我的匿名函数中使用infoWindow内容,按钮调用该函数。 我尝试了两种构造函数的方法,但我没有弄明白。 提前谢谢^^

1 个答案:

答案 0 :(得分:0)

我的老师向我展示了如何做到的简单方法。

在体内你应该添加没有任何视觉效果的div。 将标记设置为可见后,获取类.hidden-data,您可以为标记的每个属性添加任意数量的属性。

   <div class="hidden-data" data-place="" data-location="" data-address=""></div>
                marker.setVisible(true);

                $(".hidden-data").attr('data-place', place.name);
                $(".hidden-data").attr('data-location', place.geometry.location);
                $(".hidden-data").attr('data-address', place.formatted_address);

将在按钮单击时调用的jQuery函数应如下所示:

$("#addNewStore").click(function () {
    var a = $(".hidden-data").attr('data-place');
    var a = $(".hidden-data").attr('data-location');
    var a = $(".hidden-data").attr('data-address');
});