Google地图预览在地图上选择的位置

问题描述 投票:-1回答:1

我希望允许我的用户选择一个位置以及预览搜索到的位置来选择位置。

问题无法集成搜索框来预览搜索到的位置。

我正在使用的代码

<input type="text" id="search">
<div id="map"></div>

var map = document.getElementById('map');

var lp = new locationPicker(map, {
    setCurrentPosition: true,
    lat: -13.9867852,
    lng: 33.77027889
}, {
    zoom: 15 // You can set any google map options here, zoom defaults to 15
});

// Listen to button onclick event
confirmBtn.onclick = function () {
    var location = lp.getMarkerPosition();
    var location = location.lat + ',' + location.lng;
    console.log(location);
};

google.maps.event.addListener(lp.map, 'idle', function (event) {
    var location = lp.getMarkerPosition();
    var location = location.lat + ',' + location.lng;
    console.log(location);
});

如何启用输入以搜索位置并将其显示在地图上

编辑,已设法在输入上启用搜索,但无法将标记移动到所选位置

google.maps.event.addDomListener(window, 'load', initialize);

function initialize() {
    var input = document.getElementById('search');
    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.setComponentRestrictions({'country':['mw']});
    autocomplete.addListener('place_changed', function () {
        var place = autocomplete.getPlace();
        /*
        console.log(place.geometry['location'].lat());
        console.log(place.geometry['location'].lng());
        console.log(place.name);
        console.log(place.formatted_address);
        console.log(place.vicinity);
        console.log(place.url);
        console.log(place.address_components);*/
        if(!place.geometry) {
            return;
        }


        console.log(place);
    });
}
google-maps googleplacesautocomplete jquery-location-picker
1个回答
0
投票

请检出具有搜索框和地图的示例fiddle。当用户在自动完成输入中选择位置建议时,该示例然后调用getPlace()方法,然后打开一个信息窗口以显示位置详细信息。

您可以创建一个信息窗口以显示一些地点详细信息:

  <input id="search" type="text" placeholder="Enter a location">

  <div id="map"></div>
  <div id="infowindow-content">
    <img src="" width="16" height="16" id="place-icon">
    <span id="place-name" class="title"></span><br>
    <span id="place-address"></span>
  </div>

  <!-- Replace the value of the key parameter with your own API key. -->
  <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initialize" async defer></script>
  </body>

在initialize()函数中,声明您的infowindowmarker对象,然后在place_changed侦听器中设置标记位置和信息窗口内容,只要用户选择自动完成的地址建议,就会调用该侦听器。

var map;

function initialize() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: -13.9867852, lng: 33.77027889},
    zoom: 15
  });

  var input = document.getElementById('search');
  var autocomplete = new google.maps.places.Autocomplete(input);

  // Set the data fields to return when the user selects a place.
  autocomplete.setFields(
      ['address_components', 'geometry', 'icon', 'name']);

  autocomplete.setComponentRestrictions({
    'country': ['mw']
  });    

  var infowindow = new google.maps.InfoWindow();
  var infowindowContent = document.getElementById('infowindow-content');
  infowindow.setContent(infowindowContent);
  var marker = new google.maps.Marker({
    map: map,
    anchorPoint: new google.maps.Point(0, -29)
  });

  autocomplete.addListener('place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      // User entered the name of a Place that was not suggested and
      // pressed the Enter key, or the Place Details request failed.
      window.alert("No details available for input: '" + place.name + "'");
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }

    infowindowContent.children['place-icon'].src = place.icon;
    infowindowContent.children['place-name'].textContent = place.name;
    infowindowContent.children['place-address'].textContent = address;
    infowindow.open(map, marker);
  });
}  

您还可以从公共文档中签出此example

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.