将标记链接到网址Google地图

时间:2014-01-30 12:58:56

标签: javascript api google-maps google-maps-api-3

简单问题:

我正在尝试将我的标记链接到Google地图(API v3)上的外部网址。 现在可以使用http://www.google.com作为链接。

使用Javascript:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    function initialize() {
        var myLatlng = new google.maps.LatLng(-30.021664, 30.901578);
      var mapOptions = {
        zoom: 15,
        center: myLatlng,
        scrollwheel: false,
        disableDefaultUI: true
      }
    var map = new google.maps.Map(document.getElementById('ContactMap'), mapOptions);
    var image = '/Assets/Images/Icons/MapMarker.png';

      var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          animation: google.maps.Animation.DROP,
          icon: image,
          title: 'Perspex'
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

1 个答案:

答案 0 :(得分:15)

您需要添加Google地图,点击event到标记:

google.maps.event.addListener(marker, 'click', function () {
  window.location.href = 'http://www.google.com';
});

在定义marker后立即执行此操作。