Javascript 在表单提交时更新谷歌地图

Javascript 在表单提交时更新谷歌地图,javascript,jquery,forms,google-maps,google-maps-api-3,Javascript,Jquery,Forms,Google Maps,Google Maps Api 3,我一直在寻找一个解决方案几个小时,但找不到一个,我希望有人能帮助我,因为我是谷歌API的新手,所以我想做的是:我有一个表格,上面有一个地区和城市的动态下拉列表,根据用户选择,这将更新谷歌地图上的标记,显示地址、纬度和长度的信息窗口。。我的问题是提交表单,地图显示为空白。。这是我的密码: var Latlong = new google.maps.LatLng(61.10802786270912, 8.883177374999718); var map; var geocoder; var mar

我一直在寻找一个解决方案几个小时,但找不到一个,我希望有人能帮助我,因为我是谷歌API的新手,所以我想做的是:我有一个表格,上面有一个地区和城市的动态下拉列表,根据用户选择,这将更新谷歌地图上的标记,显示地址、纬度和长度的信息窗口。。我的问题是提交表单,地图显示为空白。。这是我的密码:

var Latlong = new google.maps.LatLng(61.10802786270912, 8.883177374999718);
var map;
var geocoder;
var marker;

function initialize() {
    geocoder = new google.maps.Geocoder();
          var mapOptions = {
            zoom: 5,
            center: Latlong
          }
            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

            marker = new google.maps.Marker({
                position: Latlong,
                map: map,
                draggable:true,
                title: "Drag Me!"
            }); 

            infowindow=new google.maps.InfoWindow();
            infowindow.setOptions({
                content: "<strong>Drag Marker</strong>"     
            });

            infowindow.open(map,marker);        

}

function newmap(selectedregion, selectedcity){
    var address = selectedregion+ "," +selectedcity;

    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          map.setZoom(18);
          marker.setMap(null);
          marker = new google.maps.Marker({
              map: map,
              position: results[0].geometry.location,
              draggable:true
          });
        infowindow=new google.maps.InfoWindow();
        infowindow.setOptions({
            content: "test"
        });
        infowindow.open(map,marker);

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

}

它在第一次加载时成功地调用了initialize函数,但没有成功地处理newmap函数。我避免使用这个:google.maps.event.addDomListener(窗口“加载”,初始化);因为每次表单提交时,googlemap都会转到默认的LatLng(61.10802786270912,8.883177374999718)。有人能帮我一下吗?

您正在用onload函数调用
initialize
newmap

newmap
取决于先运行initialized来实例化地理编码器和地图

这对我很有用:

$(window).load(function() {
  var selectedregion = document.getElementById('region').value;
  var selectedcity = document.getElementById('city').value;
  initialize();

  if(selectedregion == "" && selectedcity == ""){

  }else{
    newmap(selectedregion, selectedcity);
  }
});

您的代码,尤其是newmap函数与我一起工作。您还必须显示表单代码,因为可能存在问题。您的表单在提交事件时是否返回false?表单在提交时返回true,我尝试提醒(selectedregion)和提醒(selectedcity),它将值返回给我,因此表单没有任何问题。。我尝试用initialize()上的代码将newmap()函数更改为testmap(),只更改了LatLng坐标,结果很好,我就是找不到newmap()的问题所在……因为我写的newmap()也很好,所以请改天;)哇!我不知道。。这很有效!非常感谢您调查此事!我真的很感激!:)
$(window).load(function() {
  var selectedregion = document.getElementById('region').value;
  var selectedcity = document.getElementById('city').value;
  initialize();

  if(selectedregion == "" && selectedcity == ""){

  }else{
    newmap(selectedregion, selectedcity);
  }
});