在用户点击时,当用户使用谷歌地图API输入位置时,我正在尝试从".pac-item"类中提取值。
我读过以下内容
https://developers.google.com/maps/documentation/javascript/places-autocomplete。
由于 .pac-container
类是动态生成的,因此我尝试使用事件处理程序侦听.pac-container
类,但无法获取用户单击控制台的名称或位置.log或事件日志任何内容。
$(document).on("click", ".pac-container", function(event) {
var selected = $(this).val().trim();
console.log(event);
console.log("selected: " + selected);
});
I've also tried
$(document).on("click", ".pac-item", function(event) {
var selected = $(this).val().trim();
console.log(event);
console.log("selected: " + selected);
});
我期待用户点击,点击的任何位置的值。例如,如果用户键入 portland 和 .pac-container
项列表:
美国俄勒冈州波特兰波特兰国际机场站波特兰国际机场
如果用户单击波特兰,或者,我们希望将该字符串保存到我选择的变量中。
现在,没有什么是 WILL 控制台.log也没有事件是控制台.log也是如此。
好的,有点想出了一个解决方案。
在谷歌地图代码的这一部分中:
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
googleLng = searchBox.bounds.ga.j;
//give the lng some precision, as the open weather map api doesn't like
floating point numbers too long
googleLng = googleLng.toPrecision(5);
googleLat = searchBox.bounds.ma.j;
//give the lat some precision, as the open weather map api doesn't like
floating point numbers too long
googleLat = googleLat.toPrecision(5);
console.log(googleLng);
console.log(googleLat);
clicktoCoord(googleLat, googleLng);
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
console.log(searchBox(返回了一个对象,其中包含我在".pac-item"列表中所需的数据(lattitude和longitude(。有了纬度和纵向,我能够将其放入开放的天气地图 ajax api 调用中,并且能够在用户单击的任何"pac-item"列表中获取天气状况。
希望这对某人有所帮助。