打开Infowindow点击谷歌地图

时间:2014-01-28 01:42:41

标签: google-maps

如何在点击时打开标记信息窗口,而不是在页面加载时默认打开?

www.driftcoffeehouse.com

   <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.1&sensor=false"></script>
                        <script type="text/javascript">
                          //<![CDATA[
                          var map;
                          var geocoder;

                          initialize();

                          function initialize() {
                             geocoder = new google.maps.Geocoder();
                             geocoder.geocode({
                                'address': '<?php echo esc_js(get_option('mycuisine_address')); ?>',
                                'partialmatch': true}, geocodeResult);
                          }

                          function geocodeResult(results, status) {

                             if (status == 'OK' && results.length > 0) {
                                var latlng = new google.maps.LatLng(results[0].geometry.location.b,results[0].geometry.location.c);
                                var myOptions = {
                                   zoom: 13,
                                   center: results[0].geometry.location,
                                   mapTypeId: google.maps.MapTypeId.ROADMAP
                                };

                                map = new google.maps.Map(document.getElementById("gmaps-container"), myOptions);
                                   var marker = new google.maps.Marker({
                                   position: results[0].geometry.location,
                                   map: map
                                });

                                var contentString = '<div id="et-gmaps-content">'+
                                '<div id="bodyContent">'+
                                '<p><a target="_blank" href="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q='+escape(results[0].formatted_address)+'&amp;ie=UTF8&amp;view=map">'+results[0].formatted_address+'</a>'+
                                '</p>'+
                                '</div>'+
                                '</div>';



                                google.maps.event.addListener(marker, 'click', function() {
                                   infowindow.open(map,marker);
                                })

var infowindow = new google.maps.InfoWindow({
  content: contentString,
  maxWidth: 100
})

                                google.maps.event.trigger(marker, "click");

                             } else {
                                //alert("Geocode was not successful for the following reason: " + status);
                             }
                          }
                          //]]>
                        </script>

1 个答案:

答案 0 :(得分:0)

创建标记和infowindow后删除google.maps.event.trigger(marker, "click");,它应该在页面加载时停止打开infowindow。