Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击操作不适用于谷歌地图中的以下代码_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 单击操作不适用于谷歌地图中的以下代码

Javascript 单击操作不适用于谷歌地图中的以下代码,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,谁能告诉我这个代码有什么问题吗?地图将加载,但用户的单击操作不会打开“信息”窗口或将地图重新居中到单击的点 我希望当用户单击地图时,位置的地址作为信息窗口弹出 <!DOCTYPE html> <html> <head> <title>Accessing arguments in UI events</title> <meta name="viewport" content="initial-scale=1.0,

谁能告诉我这个代码有什么问题吗?地图将加载,但用户的单击操作不会打开“信息”窗口或将地图重新居中到单击的点

我希望当用户单击地图时,位置的地址作为信息窗口弹出

<!DOCTYPE html>
<html>
  <head>
    <title>Accessing arguments in UI events</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #map-canvas {
        height: 60%; width: 60%
      }
    </style>

    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry&key=MY_KEY&sensor=true">
    </script>

    <script type="text/javascript">
            var geocoder;
            var map;
            var info_win;
            var pos;
            var marker;
        function initialize() 
        {
            google.maps.visualRefresh = true;
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var mapOptions = {
            zoom: 8,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

            google.maps.event.addListener(map, 'click', function(evt) {
                pos = evt.latLng;
                map.setCenter(pos);
            geocoder.geocode({'location': pos}, function(results, status)
                    {
                        if(status == google.maps.GeocoderStatus.OK)
                            {
                                marker = new google.maps.Marker
                                ({
                                    map: map,
                                    position: pos
                                });

                                info_win = new google.maps.InfoWindow
                                ({
                                    content: results[0].formatted_address,
                                });
                                info_win.open(map,marker);  
                            }
                        else
                            {
                            alert("Could not load");
                            }
                    });
            });
         }

  </script>

</head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

访问UI事件中的参数
html,正文{
身高:100%;
边际:0px;
填充:0px
}
#地图画布{
高度:60%;宽度:60%
}
var地理编码器;
var映射;
var info_win;
var-pos;
var标记;
函数初始化()
{
google.maps.visualRefresh=true;
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(-34.397150.644);
变量映射选项={
缩放:8,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“地图画布”),mapOptions);
google.maps.event.addListener(映射,'click',函数(evt){
pos=evt.latLng;
地图设置中心(pos);
geocoder.geocode({'location':pos},函数(结果,状态)
{
if(status==google.maps.GeocoderStatus.OK)
{
marker=新的google.maps.marker
({
地图:地图,
职位:pos
});
info_win=new google.maps.InfoWindow
({
内容:结果[0]。格式化的\u地址,
});
打开信息(地图、标记);
}
其他的
{
警报(“无法加载”);
}
});
});
}

第一个问题:您正在将单击侦听器添加到初始化函数之外的映射中,因此它是在映射初始化之前添加的:

    <script type="text/javascript">
            var geocoder;
            var map;
            var info_win;
            var pos;
            var marker;
        function initialize() 
        {
            google.maps.visualRefresh = true;
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var mapOptions = {
            zoom: 8,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

        google.maps.event.addListener(map, 'click', function() {
            pos = map.getPosition();
            map.setCenter(pos);
            geocoder.geocode({'location': pos}, function(results, status)
                    {
                        marker = new google.maps.Marker
                        ({
                            map: map,
                            position: pos
                        });
                        info_win = new google.maps.InfoWindow
                        ({
                            content: results[0].formatted_address,
                        });
                    infowindow.open(map,marker);    
                    });
            });
         }

  </script>
并修复打开info_win以打开您创建的google.maps.InfoWindow的代码:

    info_win = new google.maps.InfoWindow
        ({
          content: results[0].formatted_address,
        });
    info_win.open(map,marker);    

你不是在打开它(除非你像上面那样修改了代码),而是在打开infowindow。我认为会有一个明显的javascript错误。哦。。。那个我修好了。这对我不起作用。它对你有用吗?
    info_win = new google.maps.InfoWindow
        ({
          content: results[0].formatted_address,
        });
    info_win.open(map,marker);