【发布时间】:2019-10-22 16:14:15
【问题描述】:
我想让我的用户选择一个位置以及预览搜索到的位置以选择位置。
问题 我无法集成搜索框来预览搜索到的位置。
我正在使用的代码
<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);
});
}
【问题讨论】:
-
什么标记?我没有看到您在发布的代码中创建任何标记。请提供一个 minimal reproducible example 来证明该问题。
-
如果用户在字段中输入地址,地址应该会在地图上看到 - 这就是我想要做的 - 请帮助
标签: google-maps googleplacesautocomplete jquery-location-picker