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
});
});
}
}
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.
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
Related
For known coordinates, I can set the various controls to false by specifying it in the mapOptions object
var mapOptions = {
position: new google.maps.LatLng(37.869260, -122.254811),
zoom: 1,
pov: {
heading: 165,
pitch: 0
},
addressControl: false,
panControl: false,
linksControl: false,
zoomControl: false,
};
map = new google.maps.StreetViewPanorama(mapDiv, mapOptions);
However, when using getPanoramaByLocation
latLng = new google.maps.LatLng(34,34);
radius = 500000
var sv = new google.maps.StreetViewService();
var map = new google.maps.StreetViewPanorama(mapDiv)
sv.getPanoramaByLocation(latLng, radius, function(data, status) {
if (status == google.maps.StreetViewStatus.OK) {
var nearStreetViewLocation = data.location.latLng;
map1.setPosition(nearStreetViewLocation);
map1.setZoom(1);
map1.setPov({
heading: 270,
pitch: 0
});
map1.setVisible(true);
}
});
I cannot find any methods like setPanControl or setZoomControl. How can I modify these controls when I am not declaring options in a single object?
For options where no specific setter-method is available use the set-method of MVCObject:
map1.set('panControl',true);
you may also set multiple options without the constructor by using setValues:
map1.setValues({ zoom:1,
pov:{
heading: 270,
pitch: 0
},
position:nearStreetViewLocation,
visible:true,
panControl:true,
zoomControl:true
});
How would you put a white frame behind a dynamically loaded marker icon, now, that the new Google Maps "VisualRefresh" doesn´t allow shadows anymore? 1
Open http://jsfiddle.net/FSffv/3/ with google.maps.visualRefresh = false; to see the white frame around the marker icon and google.maps.visualRefresh = true; to have it disappear.
var map;
var m_NormalImageSize = 15;
var m_NormalShadowSize = m_NormalImageSize+5;
var elevator;
var myOptions = {
zoom: 6,
center: new google.maps.LatLng(47, 8),
mapTypeId: 'terrain'
};
// turn VisualRefresh on/off
google.maps.visualRefresh = false;
map = new google.maps.Map($('#map')[0], myOptions);
var img = new Image();
img.src = "http://t2.gstatic.com/images?q=tbn:ANd9GcQUnmYVY5sWfZtBlw_IELax3W8E7-jZcCXLd2HUZYtpk_AeuK4CRnJmMHj0";
var img_ratio = img.height / img.width;
if (isNaN(img_ratio))
img_ratio = 1;
var icon_size = new google.maps.Size(m_NormalImageSize, m_NormalImageSize * img_ratio);
var shadow_size = new google.maps.Size(m_NormalShadowSize, m_NormalShadowSize * img_ratio);
var image = new google.maps.MarkerImage(
"http://t2.gstatic.com/images?q=tbn:ANd9GcQUnmYVY5sWfZtBlw_IELax3W8E7-jZcCXLd2HUZYtpk_AeuK4CRnJmMHj0",
icon_size,
new google.maps.Point(0, 0),
new google.maps.Point(-3, m_NormalImageSize * img_ratio + 3 * img_ratio),
icon_size
);
// the frame around the marker icon as a shadow
var shadow = new google.maps.MarkerImage(
"http://alsotoday.com/jpg/placemarkbackground_white.png",
shadow_size,
new google.maps.Point(0, 0),
new google.maps.Point(0, m_NormalShadowSize * img_ratio),
shadow_size
);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(47, 8),
map: map,
icon: image,
shadow: shadow,
title: "hallo"
});
One way to do this would be to use a custom HTML overlay. Then you can style it any way you like.
Here's a working example using this code:
function ImageMarker( options ) {
this.setValues( options );
this.$inner = $('<div>').css({
position: 'relative',
left: '-50%', top: '-50%',
fontSize: '1px',
lineHeight: '1px',
border: '2px solid red',
padding: '2px',
backgroundColor: 'white',
cursor: 'default'
});
this.$div = $('<div>')
.append( this.$inner )
.css({
position: 'absolute',
display: 'none'
});
};
ImageMarker.prototype = new google.maps.OverlayView;
ImageMarker.prototype.onAdd = function() {
$( this.getPanes().overlayMouseTarget ).append( this.$div );
};
ImageMarker.prototype.onRemove = function() {
this.$div.remove();
};
ImageMarker.prototype.draw = function() {
var marker = this;
var projection = this.getProjection();
var position = projection.fromLatLngToDivPixel( this.get('position') );
this.$div.css({
left: position.x,
top: position.y,
display: 'block'
})
this.$inner
.html( '<img src="' + this.get('image') + '"/>' )
.click( function( event ) {
var events = marker.get('events');
events && events.click( event );
});
};
function initialize() {
var latLng = new google.maps.LatLng( 40.708762, -74.006731 );
var map = new google.maps.Map( $('#map_canvas')[0], {
zoom: 15,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new ImageMarker({
map: map,
position: latLng,
image: 'http://cdn.sstatic.net/stackoverflow/img/favicon.ico',
events: {
click: function( event ) {
alert( 'Clicked marker' );
}
}
});
};
$( initialize );
How about having one more image with borders and make a statement like below:
if (google.maps.visualRefresh) {
image = new google.maps.MarkerImage(
"http://s24.postimg.org/lfamj2ktd/image.png",
icon_size,
new google.maps.Point(0, 0),
new google.maps.Point(-3, m_NormalImageSize * img_ratio + 3 * img_ratio),
icon_size);
}
Check this JSFiddle.
I'm trying to add a marker with a InfoWindow attached to it.
The marker is visible in both maps i.e., Street view and normal map.
But, InfoWindow only gets displayed in normal map but not when its opened in street view.
There's no error in firebug console.
The code :
var a = google.maps;
var b = {
center: new a.LatLng(parseFloat(ll[0]),parseFloat(ll[1])),
zoom: zoom,
mapTypeId: a.MapTypeId.ROADMAP,
streetViewControl: true,
mapTypeControlOptions: {
mapTypeIds: [a.MapTypeId.ROADMAP, a.MapTypeId.SATELLITE, a.MapTypeId.TERRAIN]
},
panControl: false,
zoomControlOptions: {
style: a.ZoomControlStyle.SMALL
}
};
map = new a.Map(document.getElementById("map-canvas"), b);
panorama = map.getStreetView();
panorama.setPosition(new google.maps.LatLng(42.345573,-71.098326));
panorama.setPov(/** #type {google.maps.StreetViewPov} */({
heading: 270,
pitch: 0
}));
panorama.setVisible(false);
iw = new a.InfoWindow();
a.event.addListener(map, "click", function () {
if (iw) iw.close()
});
var g = new a.Marker({
position: c,
map: map,
clickable: true,
draggable: true,
optimized: false,
raiseOnDrag: false,
zIndex: highestOrder()
});
var description = "<h2>"+document.getElementById('marker-title').value+"</h2><br/><p style='width:200px;'>"+document.getElementById('marker-desc').value+"</p>";
a.event.addListener(g, "click", function () {
actual = g;
iw.setContent(description);
if(map.getStreetView().getVisible == true) {
iw.open(map.getStreetView(), this);
}
else {
iw.open(map, this);
}
});
a.event.addListener(g, "dragstart", function () {
if (actual == g) iw.close();
z_index += 1;
g.setZIndex(highestOrder())
})
To test if the div is showing streetView imagery or a map use:
if (map.getStreetView().getVisible()) {
Not:
if(map.getStreetView().getVisible == true) {
(you aren't calling the method ...)
the click listener should be:
a.event.addListener(g, "click", function () {
actual = g;
if (map.getStreetView().getVisible()) {
iw.setContent(description);
iw.open(map.getStreetView(), this);
} else {
iw.setContent(description);
iw.open(map, this);
}
});
working example
And I've absolutely no idea why. It's the first time I try to do this with Maps API v3. The map is showing correctly, but nowhere a marker to be seen. Here's the code:
<script>
window.onload = function(evt) {
if(document.readyState === 'complete') {
var latlng = new google.maps.LatLng(50.833, 4.333);
var styles = [
{
stylers: [
{ hue: '#ffdd00' }
]
}
];
var myOptions = {
zoom: 16,
scrollwheel: false,
mapTypeControl: false,
disableDoubleClickZoom: true,
draggable: false,
navigationControl: false,
streetViewControl: false,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: styles
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var image = '/img/marker.png';
map.addMarker({
lat: 50.833,
lng: 4.333,
title: 'Here it is',
icon: image,
});
}
}
</script>
Any ideas?
No addMarker() Method.
Try new google.maps.Marker()
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'Here it is',
icon: image,
});
How can I add label to my marker if my markers are populated on ajax success each result.
map.gmap('addMarker', { 'position': new google.maps.LatLng(result.latitude, result.longitude) });
I tried like this, but with no success:
map.gmap('addMarker', {
'position': new google.maps.LatLng(result.latitude, result.longitude),
'bounds': true,
'icon': markerIcon,
'labelContent': 'A',
'labelAnchor': new google.maps.Point(result.latitude, result.longitude),
'labelClass': 'labels', // the CSS class for the label
'labelInBackground': false
});
If you just want to show label below the marker, then you can extend google maps Marker to add a setter method for label and you can define the label object by extending google maps overlayView like this..
<script type="text/javascript">
var point = { lat: 22.5667, lng: 88.3667 };
var markerSize = { x: 22, y: 40 };
google.maps.Marker.prototype.setLabel = function(label){
this.label = new MarkerLabel({
map: this.map,
marker: this,
text: label
});
this.label.bindTo('position', this, 'position');
};
var MarkerLabel = function(options) {
this.setValues(options);
this.span = document.createElement('span');
this.span.className = 'map-marker-label';
};
MarkerLabel.prototype = $.extend(new google.maps.OverlayView(), {
onAdd: function() {
this.getPanes().overlayImage.appendChild(this.span);
var self = this;
this.listeners = [
google.maps.event.addListener(this, 'position_changed', function() { self.draw(); })];
},
draw: function() {
var text = String(this.get('text'));
var position = this.getProjection().fromLatLngToDivPixel(this.get('position'));
this.span.innerHTML = text;
this.span.style.left = (position.x - (markerSize.x / 2)) - (text.length * 3) + 10 + 'px';
this.span.style.top = (position.y - markerSize.y + 40) + 'px';
}
});
function initialize(){
var myLatLng = new google.maps.LatLng(point.lat, point.lng);
var gmap = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 5,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var myMarker = new google.maps.Marker({
map: gmap,
position: myLatLng,
label: 'Hello World!',
draggable: true
});
}
</script>
<style>
.map-marker-label{
position: absolute;
color: blue;
font-size: 16px;
font-weight: bold;
}
</style>
This will work.
I doubt the standard library supports this.
But you can use the google maps utility library:
http://code.google.com/p/google-maps-utility-library-v3/wiki/Libraries#MarkerWithLabel
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var marker = new MarkerWithLabel({
position: myLatlng,
map: map,
draggable: true,
raiseOnDrag: true,
labelContent: "A",
labelAnchor: new google.maps.Point(3, 30),
labelClass: "labels", // the CSS class for the label
labelInBackground: false
});
The basics about marker can be found here: https://developers.google.com/maps/documentation/javascript/overlays#Markers
Support for single character marker labels was added to Google Maps in version 3.21 (Aug 2015). See the new marker label API.
You can now create your label marker like this:
var marker = new google.maps.Marker({
position: new google.maps.LatLng(result.latitude, result.longitude),
icon: markerIcon,
label: {
text: 'A'
}
});
If you would like to see the 1 character restriction removed, please vote for this issue.
Update October 2016:
This issue was fixed and as of version 3.26.10, Google Maps natively supports multiple character labels in combination with custom icons using MarkerLabels.
The way to do this without use of plugins is to make a subclass of google's OverlayView() method.
https://developers.google.com/maps/documentation/javascript/reference?hl=en#OverlayView
You make a custom function and apply it to the map.
function Label() {
this.setMap(g.map);
};
Now you prototype your subclass and add HTML nodes:
Label.prototype = new google.maps.OverlayView; //subclassing google's overlayView
Label.prototype.onAdd = function() {
this.MySpecialDiv = document.createElement('div');
this.MySpecialDiv.className = 'MyLabel';
this.getPanes().overlayImage.appendChild(this.MySpecialDiv); //attach it to overlay panes so it behaves like markers
}
you also have to implement remove and draw functions as stated in the API docs, or this won't work.
Label.prototype.onRemove = function() {
... // remove your stuff and its events if any
}
Label.prototype.draw = function() {
var position = this.getProjection().fromLatLngToDivPixel(this.get('position')); // translate map latLng coords into DOM px coords for css positioning
var pos = this.get('position');
$('.myLabel')
.css({
'top' : position.y + 'px',
'left' : position.x + 'px'
})
;
}
That's the gist of it, you'll have to do some more work in your specific implementation.
You can now add a class name to the marker label via google.maps.MarkerLabel interface.
For example:
const marker = new google.maps.Marker({
position: position_var,
map,
label: {
text: 'label text',
className: "my-label-class",
},
title: "Marker Title",
});
For a full list of options see the google map reference doc:
https://developers.google.com/maps/documentation/javascript/reference/marker#MarkerLabel