gpt4 book ai didi

javascript - 谷歌地图 api place_changed 事件未触发

转载 作者:行者123 更新时间:2023-11-29 15:26:42 25 4
gpt4 key购买 nike

我在我的 ASP.NET MVC 项目上使用谷歌地图 API v3。
我想向用户展示一张 map ,用户可以在上面搜索某个地方,然后选择那个地方,然后提交(非常简单)。
在这种情况下一切正常,除了当用户在 map 搜索框中输入时获得建议的地点。
这是我如何包含谷歌地图 API

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

html

<body>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<input type="button" value="hakam" id="btntest" />
<div id="map" style="width:500px; height:500px; margin-top:400px"></div>
</body>

javascript

 window.initMap = function(){
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -33.8688, lng: 151.2195 },
zoom: 13
});
var input = (document.getElementById('pac-input'));

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

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

var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map,
anchorPoint: new google.maps.Point(0, -29)
});

google.maps.event.addListener(autocomplete, 'place_changed', function () {
infowindow.close();
marker.setVisible(false);
var place = autocomplete.getPlace();
if (!place.geometry) {
window.alert("Autocomplete's returned place contains no geometry");
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.setIcon(({
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(35, 35)
}));
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(' ');
}

//infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.setContent('<div><strong>' + "Location(" + place.geometry.location.lat() + "," + place.geometry.location.lng() + ")" + '</strong><br>');
infowindow.open(map, marker);
});

}

问题

place_changed 的处理程序未触发,当我开始在 google map 搜索的文本框内书写时,未调用处理程序的代码。

经过大约 6 个小时的搜索和思考,我阅读了关于此事件的 google maps API 文档,如下所示

This event is fired when a PlaceResult is made available for a Place the user has selected. If the user enters the name of a Place that was not suggested by the control and presses the Enter key, or if a Place detail request fails, a place_changed event will be fired that contains the user input in the name property, with no other properties defined.

正如文档所说,当我单击“输入”键时,我确实触发了此事件 place_changed,并且再次,正如文档所说,我得到了包含用户输入的响应。

问题为什么 PlaceResult 不能用于用户选择的地点。 (根据谷歌文档)我做错了什么让谷歌阻止我参加这个事件?

注1
请不要在 StackOverflow 上向我提出任何关于此主题的问题,因为我从早上开始(超过 6 小时)一直在逐一阅读所有这些内容,但没有任何帮助。

注2
javascript 的示例取自谷歌地图 API 文档。

注3
按照下面评论中的建议,在加载谷歌地图库时设置语言和区域,如下所示:

<script src="https://maps.googleapis.com/maps/api/js?key=mykey&&language=ar&region=EG&libraries=places&callback=initMap" async defer></script>

没有解决我的问题。

最佳答案

此问题已在评论中得到解答,我将对此进行总结。
上面(问题中)的所有内容都是正确的,完全没有错误我只需要启用 Google Places API来自谷歌开发者控制台

这是关于 SO 的类似问题: Google Map Autocomplete API not adding address options

顺便说一句:place_changed 事件没有必要在您在搜索框中输入时触发,正如我所想的那样。

关于javascript - 谷歌地图 api place_changed 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com