将URL添加到多个标记到Wordpress中的谷歌地图

时间:2015-01-10 12:52:44

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

我想将一个谷歌地图添加到wordpress,带有多个标记,我希望每个标记都是一个网址的链接。 我对java和php了解不多,所以我做了很多研究,唯一有效的方法是:

  <script type="text/javascript">

var locations = [
      ['Rome', 41.9100711,12.5359979, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: new google.maps.LatLng(41.9100711,12.5359979),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
    clickable: true,

      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>

但我不知道如何为每个位置添加网址。有人能帮我吗?感谢。

3 个答案:

答案 0 :(得分:2)

喜欢@incredible答案, 我会提供一些代码片段,对您来说可能有点帮助..

&#13;
&#13;
var locations = [
    ['Rome', 41.9100711, 12.5359979, 4,"http://google.co.id/"],
    ['Coogee Beach', -33.923036, 151.259052, 5,"http://google.co.id/"],
    ['Cronulla Beach', -34.028249, 151.157507, 3,"http://google.co.id/"],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2,"http://google.co.id/"],
    ['Maroubra Beach', -33.950198, 151.259302, 1,"http://google.co.id/"]
];

var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: new google.maps.LatLng(41.9100711, 12.5359979),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        clickable: true,
        url:locations[i][4]
    });

    google.maps.event.addListener(marker, 'click', (function (marker, i) {
        return function () {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
            window.location.href = marker.url;
        }
    })(marker, i));
}
&#13;
<script src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<div id="map" style="width:600px; height:450px"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以将url添加为您的位置数组的第4个参数。例如:

var locations = [
      ['Rome', 41.9100711,12.5359979, 4, 'example.com'],
...
];

当你创建Marker对象时,把它作为url param。 e.g:

marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        clickable: true,
        url: locations[i][4]});

在事件听众中:

google.maps.event.addListener(marker, 'click', function() {
    window.location.href = this.url;
    ...
});

答案 2 :(得分:0)

看看我在位置ROME中做了些什么,如果有效,也要做同样的休息。

  <script type="text/javascript">

var locations = [
      ["<a href='#'>Rome</a>", 41.9100711,12.5359979, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: new google.maps.LatLng(41.9100711,12.5359979),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
    clickable: true,

      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>