gpt4 book ai didi

ajax - 谷歌地图api 3 ajax调用

转载 作者:行者123 更新时间:2023-12-01 01:26:45 26 4
gpt4 key购买 nike

我正在使用谷歌地图 API 3 和谷歌地理编码器。问题是它没有正确加载 map 我通过ajax带来数据并调用函数showAddress(elemId, address)其中 elementId 是将渲染 map 的 div id。这是谷歌地图的代码

var geocoder;
var map;

// run every mouse over
function init_gmap(elemId) {
var mapOptions = {
zoom: 6
};
map = new google.maps.Map(document.getElementById(elemId),
mapOptions)
}

// find the address
function showAddress(elemId, address) {
// todo: some caching?
init_gmap(elemId); // need to call this every time cause we're showing a new map

if (geocoder)
{
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});

var infowindow = new google.maps.InfoWindow({
map: map,
position: results[0].geometry.location,
content: address
});

google.maps.event.trigger(map, 'resize')


}
else
{

alert("Geocode was not successful for the following reason: " + status);
}
});
}
}

有什么建议吗?

谢谢。

更新代码:

<script type="text/javascript">
//<![CDATA[

var geocoder;
var map;
var lat;
var lng;
var marker;

// run every mouse over
function init_gmap(elemId,address) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status){
// console.log(results[0].geometry.location.YA);
lat = results[0].geometry.location.Ya;
lng = results[0].geometry.location.Za;
var mapOptions = {
zoom: 15,
center: new google.maps.LatLng(lat, lng),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById(elemId),
mapOptions)
});

}

// find the address
function showAddress(elemId, address) {
// todo: some caching?
init_gmap(elemId,address); // need to call this every time cause we're showing a new map
marker = "marker_"+elemId;
marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(lat, lng)
});

var infowindow = new google.maps.InfoWindow({
map: map,
position: new google.maps.LatLng(lat, lng),
content: address
});

}
</script>

最佳答案

为了初始化 map ,您需要将 center 和 mapTypeId 添加到 mapOptions 中(所有这些都是必需的,如 documentation 中所述)。例如:

    var mapOptions = {
center: new google.maps.LatLng(40.178873, -96.767578),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 6
}

关于ajax - 谷歌地图api 3 ajax调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11807507/

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