2016-10-19 171 views
1

我在使用下面的代碼給Google地圖標記添加URL時遇到了一些麻煩。Google地圖標記網址?

假設我只是想添加一個HREF(到Google.com或其他)到「wpl_map_marker_image」,所以通過點擊標記圖像它會去那裏,我會如何添加下面的代碼?

    <script type="text/javascript"> 
         function wplookInitMap() { 
          // Define the centre of the map and the position of the marker 
          var mapCenter = { 
           lat: <?php echo $wpl_map_latitude; ?>, 
           lng: <?php echo $wpl_map_longitude; ?> 
          }; 

          // Define map options 
          var mapOptions = { 
           zoom: <?php echo $wpl_map_zoom; ?>, 
           center: mapCenter, 
           draggable: false, 
           disableDefaultUI: true, 
           scrollwheel: false, 
           <?php echo (!empty($wpl_map_snazzy_style) ? 'styles: ' . $wpl_map_snazzy_style : ''); ?> 
          }; 

          // Generate the map 
          var map = new google.maps.Map(document.getElementById('contactMap'), mapOptions); 

          // Add a marker 
          var beachMarker = new google.maps.Marker({ 
           position: mapCenter, 
           map: map, 
           <?php echo (!empty($wpl_map_marker_image) ? 'icon: \'' . $wpl_map_marker_image . '\'' : ''); ?> 
          }); 

          // Center the map when the user resizes the window 
          google.maps.event.addDomListener(window, 'resize', function() { 
           map.setCenter(mapCenter); 
          }); 
         } 

         // Generate the map on page load 
         if (!window.addEventListener) { 
          window.attachEvent('load', wplookInitMap); 
         } else { 
          window.addEventListener('load', wplookInitMap, false); 
         } 
        </script> 

回答

0

試試這個,

您可以使用window.open,如下

google.maps.event.addListener(beachMarker, 'click', function() { 
    window.open("http://google.com/"); 
}); 

jsFiddel演示:

http://jsfiddle.net/Lw6tF/14/

0

阿拜,是它被稱爲 「beachMarker」在我的情況下,但你的代碼工作。

如果有人需要並在未來讀取它,在這裏你去:

<script type="text/javascript"> 
          function wplookInitMap() { 
           // Define the centre of the map and the position of the marker 
           var mapCenter = { 
            lat: <?php echo $wpl_map_latitude; ?>, 
            lng: <?php echo $wpl_map_longitude; ?> 
           }; 

           // Define map options 
           var mapOptions = { 
            zoom: <?php echo $wpl_map_zoom; ?>, 
            center: mapCenter, 
            draggable: false, 
            disableDefaultUI: true, 
            scrollwheel: false, 
            <?php echo (!empty($wpl_map_snazzy_style) ? 'styles: ' . $wpl_map_snazzy_style : ''); ?> 
           }; 

           // Generate the map 
           var map = new google.maps.Map(document.getElementById('contactMap'), mapOptions); 

           // Add a marker 
           var beachMarker = new google.maps.Marker({ 
            position: mapCenter, 
            map: map, 
            <?php echo (!empty($wpl_map_marker_image) ? 'icon: \'' . $wpl_map_marker_image . '\'' : ''); ?> 
           }); 

            google.maps.event.addListener(beachMarker, 'click', function() { 
            window.open("http://google.com/"); 
            }) ; 

           // Center the map when the user resizes the window 
           google.maps.event.addDomListener(window, 'resize', function() { 
            map.setCenter(mapCenter); 
           }); 
          } 

          // Generate the map on page load 
          if (!window.addEventListener) { 
           window.attachEvent('load', wplookInitMap); 
          } else { 
           window.addEventListener('load', wplookInitMap, false); 
          } 
         </script>