在 wordpress 中将 url 添加到 google 地图的多个标记
Add url to multiple markers to google map in wordpress
我想添加一个 google 映射到 wordpress,带有多个标记,我希望每个标记都是 link 到 url。
我对 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>
但我不知道如何为每个位置添加 url。有人能帮我吗?谢谢
您可以将 url 作为第 4 个参数添加到位置数组。例如:
var locations = [
['Rome', 41.9100711,12.5359979, 4, 'example.com'],
...
];
并且当您创建 Marker 对象时,将其作为 url 参数。例如:
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;
...
});
看看我在罗马外景地做了什么,如果行的话,照样休息。
<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>
喜欢@incredible 的回答,
我会提供一些代码片段,可能对你有一点帮助..
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));
}
<script src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<div id="map" style="width:600px; height:450px"></div>
我想添加一个 google 映射到 wordpress,带有多个标记,我希望每个标记都是 link 到 url。 我对 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>
但我不知道如何为每个位置添加 url。有人能帮我吗?谢谢
您可以将 url 作为第 4 个参数添加到位置数组。例如:
var locations = [
['Rome', 41.9100711,12.5359979, 4, 'example.com'],
...
];
并且当您创建 Marker 对象时,将其作为 url 参数。例如:
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;
...
});
看看我在罗马外景地做了什么,如果行的话,照样休息。
<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>
喜欢@incredible 的回答, 我会提供一些代码片段,可能对你有一点帮助..
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));
}
<script src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<div id="map" style="width:600px; height:450px"></div>