问题
I have a Google Map (v3) that I am placing several markers into, and want to be able to click on the markers and pull up a Fancybox iFrame. I can't figure out how to pull the URL from the marker into Fancybox.
I can see the Fancybox firing - the popup window appears breifly and then the link is pulled up as a new page over top of the current page.
Here is a snippet of the relevant code:
var sites = [
['Mount Evans', 39.58108, -105.63535, 4, 'www.myurl-1.com'],
['Irving Homestead', 40.315939, -105.440630, 2, 'wwww.myurl-2.com'],
['Badlands National Park', 43.785890, -101.90175, 1, 'www.myurl-3.com'],
['Flatirons in the Spring', 39.99948, -105.28370, 3, 'www.myurl-4.com']
];
function setMarkers(map, markers) {
for (var i = 0; i < markers.length; i++) {
var sites = markers[i];
var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
var marker = new google.maps.Marker({
position: siteLatLng,
map: map,
title: sites[0],
zIndex: sites[3],
url: sites[4]
});
google.maps.event.addListener(marker, 'click', function() {
$.fancybox({
href : function() {
window.location.href = marker.url;
},
width : 1000,
maxHeight : 666,
fitToView : true,
autoSize : false,
type: 'iframe',
padding: 0,
openEffect : 'elastic',
openSpeed : 150,
aspectRatio : true,
closeEffect : 'elastic',
closeSpeed : 150,
closeClick : true,
iframe : { scrolling : 'no'
},
preload : true
});
});
}
}
回答1:
Here is the final code I used to solve this:
var sites = [
['Mount Evans', 39.58108, -105.63535, 4, 'www.myurl-1.com'],
['Irving Homestead', 40.315939, -105.440630, 2, 'wwww.myurl-2.com'],
['Badlands National Park', 43.785890, -101.90175, 1, 'www.myurl-3.com'],
['Flatirons in the Spring', 39.99948, -105.28370, 3, 'www.myurl-4.com']
];
function setMarkers(map, markers) {
for (var i = 0; i < markers.length; i++) {
var sites = markers[i];
var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
var marker = new google.maps.Marker({
position: siteLatLng,
map: map,
title: sites[0],
zIndex: sites[3],
href: sites[4]
});
google.maps.event.addListener(marker, 'click', function() {
$.fancybox({
href : sites[4],
width : 1000,
maxHeight : 666,
fitToView : true,
autoSize : false,
type: 'iframe',
padding: 0,
openEffect : 'elastic',
openSpeed : 150,
aspectRatio : true,
closeEffect : 'elastic',
closeSpeed : 150,
closeClick : true,
iframe : { scrolling : 'no'
},
preload : true
});
});
}
}
Just referenced href in the marker options and referred to in the fancybox call.
回答2:
Check also this post Open DIV as fancy box Google Map Marker
$("a#fancyBoxLink").fancybox({
'href' : '#myDivID',
'titleShow' : false,
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
});
Fancybox with Google Map Marker
来源:https://stackoverflow.com/questions/18002429/fancybox-iframe-from-google-map-v3-marker-click