Javascript 无限递归与谷歌地图

Javascript 无限递归与谷歌地图,javascript,google-maps,google-maps-api-3,recursion,Javascript,Google Maps,Google Maps Api 3,Recursion,我有一个输入字段,用户在其中输入地址。Google Maps API自动完成它,然后重新定位地图标记,并将地图移动到该位置 var map_location = new google.maps.LatLng(45.815015, 15.981912); var marker; var map; var autocomplete; function initialize() { var mapOptions = { zoom: 13, center: ma

我有一个输入字段,用户在其中输入地址。Google Maps API自动完成它,然后重新定位地图标记,并将地图移动到该位置

var map_location = new google.maps.LatLng(45.815015, 15.981912);
var marker;
var map;
var autocomplete;

function initialize() {
    var mapOptions = {
        zoom: 13,
        center: map_location
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    marker = new google.maps.Marker({
        map: map,
        draggable: true,
        position: map_location
    });

    google.maps.event.addListener(marker, 'dragend', function (event) {
        document.getElementById("lat_hidden").value = this.getPosition().lat();
        document.getElementById("long_hidden").value = this.getPosition().lng();
    });

    autocomplete = new google.maps.places.Autocomplete(
        document.getElementById('autocomplete'), 
        {types: ['geocode']});

    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        map_location = new google.maps.LatLng(
            parseFloat(document.getElementById("lat_hidden").value),
            parseFloat(document.getElementById("long_hidden").value)
        );
        marker.setPosition(map_location);
        map.panTo(map_location);
  });
}
最后一行代码导致无限递归。我还尝试了
.setCenter
。Firefox将递归追溯到maps.gstatic.com脚本,该脚本一直在调用自己的两个函数


问题的原因是什么?我如何解决它?

您没有在
place\u changed
事件上设置lat\u hidden和long\u hidden字段。因此,此代码不会创建有效的
google.maps.LatLng()

工作代码段:

var map_location=new google.maps.LatLng(45.815015,15.981912);
var标记;
var映射;
var自动完成;
函数初始化(){
变量映射选项={
缩放:13,
中心:地图位置
};
map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
marker=新的google.maps.marker({
地图:地图,
真的,
位置:地图位置
});
google.maps.event.addListener(标记'dragend',函数(事件){
document.getElementById(“lat_hidden”).value=this.getPosition().lat();
document.getElementById(“long_hidden”).value=this.getPosition().lng();
});
autocomplete=new google.maps.places.autocomplete(
document.getElementById('autocomplete'){
类型:['geocode']
});
google.maps.event.addListener(自动完成,'place\u changed',函数(){
document.getElementById(“lat_hidden”).value=autocomplete.getPlace().geometry.location.lat();
document.getElementById(“long_hidden”).value=autocomplete.getPlace().geometry.location.lng();
map_location=新建google.maps.LatLng(
parseFloat(document.getElementById(“lat_hidden”).value),
parseFloat(document.getElementById(“long_hidden”).value)
);
标记器。设置位置(地图位置);
panTo地图(地图位置);
});
}
google.maps.event.addDomListener(窗口“加载”,初始化)
html,
身体,
#地图画布{
高度:500px;
宽度:500px;
边际:0px;
填充:0px
}

设置中心不是为您工作吗?
    map_location = new google.maps.LatLng(
        parseFloat(document.getElementById("lat_hidden").value),
        parseFloat(document.getElementById("long_hidden").value)
    );