Google maps 如何在谷歌地图信息窗口中触发事件?

Google maps 如何在谷歌地图信息窗口中触发事件?,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我希望地图上有一个默认标记来指示地址,而用户可以将用户标记拖到他们认为正确的位置。(适用于某些地图校正情况…) 如果我将onclick添加到按钮,我会得到函数未定义错误,就像上面的代码一样,在这种情况下,它会显示“clsMark not defined” 所以我完全不知道如何让按钮在信息窗口中工作。请帮我解决…您在传递给geocoder.geocode()的匿名函数中声明了clsMark()函数,这就是为什么它在全局范围内不可用(onclick处理程序正在查找它) 尝试在geocoder.geo

我希望地图上有一个默认标记来指示地址,而用户可以将用户标记拖到他们认为正确的位置。(适用于某些地图校正情况…)

如果我将onclick添加到按钮,我会得到函数未定义错误,就像上面的代码一样,在这种情况下,它会显示“clsMark not defined”


所以我完全不知道如何让按钮在信息窗口中工作。请帮我解决…

您在传递给geocoder.geocode()的匿名函数中声明了clsMark()函数,这就是为什么它在全局范围内不可用(onclick处理程序正在查找它)

尝试在geocoder.geocode调用之前移动函数clsMark(),使其对onclick处理程序可见

至于您尝试使用“addListener(buttonID,…”,请注意addListener不适用于ID,您必须提供“object”作为第一个参数,因此这只适用于使用google maps对象

<script type="text/javascript"><!--
var geocoder;
var map;
var point;
function mapLoad() {
geocoder = new google.maps.Geocoder();
var myOptions = {
  zoom: 16,
  mapTypeControl: false, 
  mapTypeId: google.maps.MapTypeId.ROADMAP
}

map = new google.maps.Map(document.getElementById("map"), myOptions);
var address = "LS2 2RD"; //default postcode
geocoder.geocode( { 'address':address,region:"UK",language:"en"}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    point=results[0].geometry.location;
    map.setCenter(point);
    var defaultMarker = new google.maps.Marker({map: map, position: point}); //default marker
    var marker = new google.maps.Marker({
        map: map, 
        position: point,
  draggable: true
    }); //The marker for users to drag

    var DefaultContent ='<h4>Default Location</h4><h6>You can drag it to anywhere you think it is right or confirm here is the right place</h6><div class="confirm"><input type="button" value="This location is correct"></div>'
    var DefaultInfo = new google.maps.InfoWindow({content: DefaultContent,maxWidth:200});
    DefaultInfo.open(map,marker); //default infowindow

      var TargetContent='<h4>New Location</h4><h6>Are you sure here is the right place?</h6><div class="confirm"><input type="button" value="Yes, I confirm" />'+
      '<input type="button" value="Cancel" onclick="clsMark()" /></div>'; //here cancel button is not working
      var infobox = new google.maps.InfoWindow({content: TargetContent}); //after drag ended, the new infowindow

google.maps.event.addListener(marker, 'drag', function(){
        DefaultInfo.close();
        });
google.maps.event.addListener(marker, 'dragend', function() {
      infobox.open(map, marker);
    });

function clsMark(){if (infobox != null) {infobox.close();}//if user cancel, clear the infowindow, and put the marker back to default position
        marker.setPosition(point);}

  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
google.maps.event.addListener(buttonID, 'click', function(){...}