Marker shadows in Google Maps v3 - google-maps

I'm looking for a way to make marker shadows work with the "visual refresh" that's coming to Google Maps, but can't seem to.
I suppose the following is the recommended way to do it:
https://developers.google.com/maps/tutorials/customizing/custom-markers
Can anyone see the marker shadows in the examples on that tutorial? I can't.
What are you guys doing to make this work?
Thanks in advance!

One option for adding shadows to post visual refresh Google Maps Javascript API v3 maps:
create a Custom Overlay to hold the shadow image attached to the "overlayShadow" pane
add one for every marker
proof of concept
Add markers with shadows like this:
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: getMarkerImage(beach[4]),
shape: iconShape,
title: beach[0],
zIndex: Math.round(myLatLng.lat()*-100000)<<5
});
var shadow = new MarkerShadow(myLatLng, iconShadow, map);
marker.bindTo('map',shadow,'map');
MarkerShadow code (modified from Google's Custom Overlay example):
MarkerShadow.prototype = new google.maps.OverlayView();
/** #constructor */
function MarkerShadow(position, options, map) {
// Initialize all properties.
this.posn_ = position;
this.map_ = map;
if (typeof(options) == "string") {
this.image = options;
} else {
this.options_ = options;
if (!!options.size) this.size_ = options.size;
if (!!options.url) this.image_ = options.url;
}
// Define a property to hold the image's div. We'll
// actually create this div upon receipt of the onAdd()
// method so we'll leave it null for now.
this.div_ = null;
// Explicitly call setMap on this overlay.
this.setMap(map);
}
/**
* onAdd is called when the map's panes are ready and the overlay has been
* added to the map.
*/
MarkerShadow.prototype.onAdd = function() {
// if no url, return, nothing to do.
if (!this.image_) return;
var div = document.createElement('div');
div.style.borderStyle = 'none';
div.style.borderWidth = '0px';
div.style.position = 'absolute';
// Create the img element and attach it to the div.
var img = document.createElement('img');
img.src = this.image_;
img.style.width = this.options_.size.x + 'px';
img.style.height = this.options_.size.y +'px';
img.style.position = 'absolute';
div.appendChild(img);
this.div_ = div;
// Add the element to the "overlayLayer" pane.
var panes = this.getPanes();
panes.overlayShadow.appendChild(div);
};
MarkerShadow.prototype.draw = function() {
// if no url, return, nothing to do.
if (!this.image_) return;
// We use the coordinates of the overlay to peg it to the correct position
// To do this, we need to retrieve the projection from the overlay.
var overlayProjection = this.getProjection();
var posn = overlayProjection.fromLatLngToDivPixel(this.posn_);
// Resize the image's div to fit the indicated dimensions.
if (!this.div_) return;
var div = this.div_;
if (!!this.options_.anchor) {
div.style.left = Math.floor(posn.x-this.options_.anchor.x) + 'px';
div.style.top = Math.floor(posn.y-this.options_.anchor.y) + 'px';
}
if (!!this.options_.size) {
div.style.width = this.size_.x + 'px';
div.style.height = this.size_.y + 'px';
}
};
// The onRemove() method will be called automatically from the API if
// we ever set the overlay's map property to 'null'.
MarkerShadow.prototype.onRemove = function() {
if (!this.div_) return;
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
};

Why not create an extra marker with lower z-index?
createMarkerShadow = function(map, data) {
var latLng = new google.maps.LatLng(data.latitude, data.longitude);
var markerShadow = new google.maps.Marker({
clickable: false,
position: latLng,
map: map,
icon:{
url: '/frontend/img/icons/google-map-marker-shadow.svg',
//The size image file.
size: new google.maps.Size(225, 120),
//The point on the image to measure the anchor from. 0, 0 is the top left.
origin: new google.maps.Point(0, 0),
//The x y coordinates of the anchor point on the marker. e.g. If your map marker was a drawing pin then the anchor would be the tip of the pin.
anchor: new google.maps.Point(115, 82)
},
zIndex: (Math.round(latLng.lat()*-100000)<<5)-1
});
return markerShadow;
};
setMarkerShadows = function (map, locations, bound) {
for (var i = 0; i < locations.length; i++) {
var data = locations[i];
var markerShadow = createMarkerShadow(map, data);
bound.extend(markerShadow.getPosition());
}
};
bound = new google.maps.LatLngBounds();

Related

Why isn't Google Maps' Custom Overlay working? (In their docs)

I'm currently trying to implement Custom Overlays and am looking at Google's docs. However, their example doesn't seem to be working which is making it quite difficult.
Docs: https://developers.google.com/maps/documentation/javascript/examples/overlay-simple
It appears that the "onAdd" callback is never called, even though these docs say that onAdd will be called after setMap is called.
Anyone know what's wrong with their example and why it's not working?
geocodezip commented and their answer fixes it :).
"They broke it when the translated it to the new format. fiddle. The pieces that depend on the API need to be in the initMap function"
See below for fiddle contents:
(function(exports) {
"use strict";
// This example creates a custom overlay called USGSOverlay, containing
// a U.S. Geological Survey (USGS) image of the relevant area on the map.
// Set the custom overlay object's prototype to a new instance
// of OverlayView. In effect, this will subclass the overlay class therefore
// it's simpler to load the API synchronously, using
// google.maps.event.addDomListener().
// Note that we set the prototype to an instance, rather than the
// parent class itself, because we do not wish to modify the parent class.
// Initialize the map and the custom overlay.
function initMap() {
USGSOverlay.prototype = new google.maps.OverlayView();
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 11,
center: {
lat: 62.323907,
lng: -150.109291
},
mapTypeId: "satellite"
});
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(62.281819, -150.287132),
new google.maps.LatLng(62.400471, -150.005608)
); // The photograph is courtesy of the U.S. Geological Survey.
var srcImage =
"https://developers.google.com/maps/documentation/" +
"javascript/examples/full/images/talkeetna.png"; // The custom USGSOverlay object contains the USGS image,
// the bounds of the image, and a reference to the map.
/** #constructor */
function USGSOverlay(bounds, image, map) {
// Initialize all properties.
this.bounds_ = bounds;
this.image_ = image;
this.map_ = map; // Define a property to hold the image's div. We'll
// actually create this div upon receipt of the onAdd()
// method so we'll leave it null for now.
this.div_ = null; // Explicitly call setMap on this overlay.
this.setMap(map);
}
/**
* onAdd is called when the map's panes are ready and the overlay has been
* added to the map.
*/
USGSOverlay.prototype.onAdd = function() {
var div = document.createElement("div");
div.style.borderStyle = "none";
div.style.borderWidth = "0px";
div.style.position = "absolute"; // Create the img element and attach it to the div.
var img = document.createElement("img");
console.log(this.image_);
img.src = this.image_;
img.style.width = "100%";
img.style.height = "100%";
img.style.position = "absolute";
div.appendChild(img);
this.div_ = div; // Add the element to the "overlayLayer" pane.
var panes = this.getPanes();
panes.overlayLayer.appendChild(div);
};
USGSOverlay.prototype.draw = function() {
// We use the south-west and north-east
// coordinates of the overlay to peg it to the correct position and size.
// To do this, we need to retrieve the projection from the overlay.
var overlayProjection = this.getProjection(); // Retrieve the south-west and north-east coordinates of this overlay
// in LatLngs and convert them to pixel coordinates.
// We'll use these coordinates to resize the div.
var sw = overlayProjection.fromLatLngToDivPixel(
this.bounds_.getSouthWest()
);
var ne = overlayProjection.fromLatLngToDivPixel(
this.bounds_.getNorthEast()
); // Resize the image's div to fit the indicated dimensions.
var div = this.div_;
div.style.left = sw.x + "px";
div.style.top = ne.y + "px";
div.style.width = ne.x - sw.x + "px";
div.style.height = sw.y - ne.y + "px";
}; // The onRemove() method will be called automatically from the API if
// we ever set the overlay's map property to 'null'.
USGSOverlay.prototype.onRemove = function() {
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
};
exports.overlay = new USGSOverlay(bounds, srcImage, map);
exports.USGSOverlay = USGSOverlay;
}
exports.initMap = initMap;
})((this.window = this.window || {}));

Google maps how to calculate south west and north east bound for overlay

While displaying an overlay over google maps using the API. I am googling for last three days how to calculate the South West and North east bounds for an overlay of size 100X100. I found millions of examples but the bounds were written hard coded.
My input is Center Latitude and Longitude and a 100x100 image.
The code I found is:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 62.323907, lng: -150.109291},
mapTypeId: google.maps.MapTypeId.SATELLITE
});
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(SE),
new google.maps.LatLng(NE));
// The photograph is courtesy of the U.S. Geological Survey.
var srcImage = 'https://developers.google.com/maps/documentation/javascript/';
srcImage += 'examples/full/images/talkeetna.png';
overlay = new USGSOverlay(bounds, srcImage, map);
}
I am woundering how these bounds were calculated?
SE and NE
To calculate the bounds (specifically a google.maps.LatLngBounds object) that is 100px x 100px centered at the center of the bounds of that overlay:
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(62.281819, -150.287132),
new google.maps.LatLng(62.400471, -150.005608));
find the pixel position of the center:
var center = bounds.getCenter();
var centerPt = overlay.getProjection().fromLatLngToDivPixel(center);
find the pixel point 50px up and 50px right of it
var topRightPt = new google.maps.Point(centerPt.x+50, centerPt.y+50);
find the pixel point 50px down and 50px left of it
var botLeftPt = new google.maps.Point(centerPt.x-50, centerPt.y-50);
translate those back to latitude and longitude coordinates
var sw = overlay.getProjection().fromDivPixelToLatLng(botLeftPt);
var ne = overlay.getProjection().fromDivPixelToLatLng(topRightPt);
proof of concept fiddle
overlay = new USGSOverlay(bounds, srcImage, map);
google.maps.event.addListener(overlay, 'projection_changed', function() {
var centerPt = overlay.getProjection().fromLatLngToDivPixel(center);
var topRightPt = new google.maps.Point(centerPt.x+50, centerPt.y+50);
var botLeftPt = new google.maps.Point(centerPt.x-50, centerPt.y-50);
var sw = overlay.getProjection().fromDivPixelToLatLng(botLeftPt);
var ne = overlay.getProjection().fromDivPixelToLatLng(topRightPt);
var overlayBnds = new google.maps.LatLngBounds(sw,ne);
var rectangle = new google.maps.Rectangle({
map: map,
bounds: overlayBnds
})
});
code snippet:
google.maps.event.addDomListener(window, "load", initMap);
// This example creates a custom overlay called USGSOverlay, containing
// a U.S. Geological Survey (USGS) image of the relevant area on the map.
// Set the custom overlay object's prototype to a new instance
// of OverlayView. In effect, this will subclass the overlay class therefore
// it's simpler to load the API synchronously, using
// google.maps.event.addDomListener().
// Note that we set the prototype to an instance, rather than the
// parent class itself, because we do not wish to modify the parent class.
var overlay;
USGSOverlay.prototype = new google.maps.OverlayView();
// Initialize the map and the custom overlay.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {
lat: 62.323907,
lng: -150.109291
},
mapTypeId: 'satellite'
});
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(62.281819, -150.287132),
new google.maps.LatLng(62.400471, -150.005608));
var center = bounds.getCenter();
// The photograph is courtesy of the U.S. Geological Survey.
var srcImage = 'https://developers.google.com/maps/documentation/' +
'javascript/examples/full/images/talkeetna.png';
// The custom USGSOverlay object contains the USGS image,
// the bounds of the image, and a reference to the map.
overlay = new USGSOverlay(bounds, srcImage, map);
google.maps.event.addListener(overlay, 'projection_changed', function() {
var centerPt = overlay.getProjection().fromLatLngToDivPixel(center);
var topRightPt = new google.maps.Point(centerPt.x + 50, centerPt.y + 50);
var botLeftPt = new google.maps.Point(centerPt.x - 50, centerPt.y - 50);
var sw = overlay.getProjection().fromDivPixelToLatLng(botLeftPt);
var ne = overlay.getProjection().fromDivPixelToLatLng(topRightPt);
var overlayBnds = new google.maps.LatLngBounds(sw, ne);
var rectangle = new google.maps.Rectangle({
map: map,
bounds: overlayBnds
})
});
}
/** #constructor */
function USGSOverlay(bounds, image, map) {
// Initialize all properties.
this.bounds_ = bounds;
this.image_ = image;
this.map_ = map;
// Define a property to hold the image's div. We'll
// actually create this div upon receipt of the onAdd()
// method so we'll leave it null for now.
this.div_ = null;
// Explicitly call setMap on this overlay.
this.setMap(map);
}
/**
* onAdd is called when the map's panes are ready and the overlay has been
* added to the map.
*/
USGSOverlay.prototype.onAdd = function() {
var div = document.createElement('div');
div.style.borderStyle = 'none';
div.style.borderWidth = '0px';
div.style.position = 'absolute';
// Create the img element and attach it to the div.
var img = document.createElement('img');
img.src = this.image_;
img.style.width = '100%';
img.style.height = '100%';
img.style.position = 'absolute';
div.appendChild(img);
this.div_ = div;
// Add the element to the "overlayLayer" pane.
var panes = this.getPanes();
panes.overlayLayer.appendChild(div);
};
USGSOverlay.prototype.draw = function() {
// We use the south-west and north-east
// coordinates of the overlay to peg it to the correct position and size.
// To do this, we need to retrieve the projection from the overlay.
var overlayProjection = this.getProjection();
// Retrieve the south-west and north-east coordinates of this overlay
// in LatLngs and convert them to pixel coordinates.
// We'll use these coordinates to resize the div.
var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
// Resize the image's div to fit the indicated dimensions.
var div = this.div_;
div.style.left = sw.x + 'px';
div.style.top = ne.y + 'px';
div.style.width = (ne.x - sw.x) + 'px';
div.style.height = (sw.y - ne.y) + 'px';
};
// The onRemove() method will be called automatically from the API if
// we ever set the overlay's map property to 'null'.
USGSOverlay.prototype.onRemove = function() {
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
};
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>

Display Text inside a circle marker

I want to display a circle like in this sample:
https://developers.google.com/maps/documentation/javascript/examples/circle-simple?hl=en
I do not want to use clustered markers as I really just need to display some circles with a
number inside.
UPDATE
That is my code:
$(document).ready(function() {
//Google maps API initialisation
var overlay;
USGSOverlay.prototype = new google.maps.OverlayView();
/* constructor */
function USGSOverlay(bounds, map, alarmnumber) {
// Now initialize all properties.
this.bounds_ = bounds;
this.map_ = map;
this.alarmnumber_ = alarmnumber;
// We define a property to hold the image's div. We'll
// actually create this div upon receipt of the onAdd()
// method so we'll leave it null for now.
this.div_ = null;
// Explicitly call setMap on this overlay
this.setMap(map);
}
USGSOverlay.prototype.onAdd = function() {
// Note: an overlay's receipt of onAdd() indicates that
// the map's panes are now available for attaching
// the overlay to the map via the DOM.
// Create the DIV and set some basic attributes.
var div = document.createElement('div');
div.style.borderStyle = 'none';
div.style.borderWidth = '0px';
div.style.position = 'absolute';
div.style.border = '1px solid #000';
div.style.fontWeight = 'bold';
div.style.backgroundColor = '#fff';
$(div).attr('id', 'alarmDiv' + this.alarmnumber_);
$(div).addClass("circle");
$(div).addClass("center");
// Set the overlay's div_ property to this DIV
this.div_ = div;
this.div_.innerHTML = this.alarmnumber_;
// We add an overlay to a map via one of the map's panes.
// We'll add this overlay to the overlayLayer pane.
var panes = this.getPanes();
panes.overlayLayer.appendChild(div);
};
USGSOverlay.prototype.draw = function() {
// Size and position the overlay. We use a southwest and northeast
// position of the overlay to peg it to the correct position and size.
// We need to retrieve the projection from this overlay to do this.
var overlayProjection = this.getProjection();
// Retrieve the southwest and northeast coordinates of this overlay
// in latlngs and convert them to pixels coordinates.
// We'll use these coordinates to resize the DIV.
var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
// Resize the image's DIV to fit the indicated dimensions.
var div = this.div_;
div.style.left = (sw.x - 20) + 'px';
div.style.top = (ne.y - 20) + 'px';
div.style.width = 40 + 'px';
div.style.height = 40 + 'px';
};
var element = document.getElementById("map");
var map = new google.maps.Map(element, {
center: new google.maps.LatLng(48.705236, 9.128566), // Latitude/Breitengrad , Longitude/Längengrad
zoom: 18,
mapTypeId: google.maps.MapTypeId.ROADMAP, // Google maps provider
//mapTypeId: "OSM", // OpenStreetMap provider
mapTypeControl: false,
streetViewControl: false
});

Can't prevent all marker overlays from overlapping markers google maps api v3

I have a google map using api version 3. I want to create markers with a custom icon and numbered labels. I've been trying to use what seems to be the most accepted method for this, the "labels.js" solution that I've provided below. However, no matter what I try, all the numbered overlays overlap all of the markers (despite me setting the marker and the label with the same zIndex). See the screenshot provided to see what I'm talking about. If you take a look at markers 14 and 15 in the screen, you'll see that the 15 label overlaps the 14 marker, but it shouldn't, it should be underneath the 14 marker.
http://i.imgur.com/QoYqcHJ.jpg
Many discussions about properly overlapping with custom overlays revolve around the line of code:
var pane = this.getPanes().overlayImage;
However, I have this in place. I'm setting each label overlay and marker pair to the same zIndex, and the properly overlapping markers proves that this zIndex increment is working. I've provided all of my code below, and have run into a brick wall. I've tried everything possible with no luck. Assume all variables have been properly declared.
label.js:
/* START label.js */
// Define the overlay, derived from google.maps.OverlayView
function Label(opt_options) {
// Initialization
this.setValues(opt_options);
// Here go the label styles
var span = this.span_ = document.createElement('span');
span.style.cssText = 'position: relative;' +
'white-space: nowrap;color:#666666;' +
'font-family: Arial; font-weight: bold;' +
'font-size: 11px;';
var div = this.div_ = document.createElement('div');
div.appendChild(span);
div.style.cssText = 'position: absolute; display: none;';
};
Label.prototype = new google.maps.OverlayView;
Label.prototype.onAdd = function () {
var pane = this.getPanes().overlayImage;
pane.appendChild(this.div_);
// Ensures the label is redrawn if the text or position is changed.
var me = this;
this.listeners_ = [
google.maps.event.addListener(this, 'position_changed',
function () { me.draw(); }),
google.maps.event.addListener(this, 'text_changed',
function () { me.draw(); }),
google.maps.event.addListener(this, 'zindex_changed',
function () { me.draw(); })
];
};
// Implement onRemove
Label.prototype.onRemove = function () {
this.div_.parentNode.removeChild(this.div_);
// Label is removed from the map, stop updating its position/text.
for (var i = 0, I = this.listeners_.length; i < I; ++i) {
google.maps.event.removeListener(this.listeners_[i]);
}
};
// Implement draw
Label.prototype.draw = function () {
var projection = this.getProjection();
var div = this.div_;
// Some custom code to properly get the offset for the numbered label for each marker
var labelOffset;
if (parseInt(this.get('text').toString()) < 10) labelOffset = new google.maps.Point(6, -35);
else labelOffset = new google.maps.Point(9, -35);
var point1 = this.map.getProjection().fromLatLngToPoint(
(this.get('position') instanceof google.maps.LatLng) ? this.get('position') : this.map.getCenter()
);
var point2 = new google.maps.Point(
((typeof (labelOffset.x) == 'number' ? labelOffset.x : 0) / Math.pow(2, map.getZoom())) || 0,
((typeof (labelOffset.y) == 'number' ? labelOffset.y : 0) / Math.pow(2, map.getZoom())) || 0
);
var offSetPosition = this.map.getProjection().fromPointToLatLng(new google.maps.Point(
point1.x - point2.x,
point1.y + point2.y
));
var position = projection.fromLatLngToDivPixel(offSetPosition);
// End custom code
div.style.left = position.x + 'px';
div.style.top = position.y + 'px';
div.style.display = 'block';
div.style.zIndex = this.get('zIndex'); //ALLOW LABEL TO OVERLAY MARKER
this.span_.innerHTML = this.get('text').toString();
};
/* END label.js */
Code to create map with markers:
var mapOptions = {
zoom: myZoom,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false
};
map = new google.maps.Map(document.getElementById("gmap"), mapOptions);
/* Insert logic here to iterate and add each marker */
// This function is called for every marker, i increases by 1 each call
function addMarker(latlng, mylabel, isShowroom, data, type, i) {
var markerImage;
var labelColor = '#666666';
if (isShowroom) {
markerImage = 'http://www.subzero-wolf.com/common/images/locator/pin-showroom.png';
} else {
if (type == 'service') {
markerImage = '/common/images/locator/pin-dealer.png';
} else if (type == 'parts') {
markerImage = '/common/images/locator/pin-parts.png';
} else {
markerImage = '/common/images/locator/pin-dealer.png';
}
}
var myMarker = new google.maps.Marker({
position: latlng,
draggable: false,
clickable: true,
map: map,
icon: markerImage,
zIndex: isShowroom ? 9999 : i
});
var html = "test content"
myMarker['isShowroom'] = isShowroom;
myMarker['infowindow'] = new google.maps.InfoWindow({
content: html
});
google.maps.event.addListener(myMarker, 'click', function() {
this['infowindow'].open(map, this);
});
// Dont show a label for the showroom because this is the marker with the star icon, no number needed
if (!isShowroom) {
var label = new Label({
map: map
});
label.set('zIndex', i);
label.bindTo('position', myMarker, 'position');
label.set('text', mylabel);
}
markerArray.push(myMarker);
}

How to forbid a google api infoWindow to open at page loading

I found this script.
It allows to create a info window with google api. I modified it to fit my needs but I can't forbid it to open all infowindows automatically when I load the page (I only want a single info window appears when the costumers click on the marker).
Someone can help me?
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=true">
</script>
<script type="text/javascript">
var browserSupportFlag = new Boolean();
/* An InfoBox is like an info window, but it displays
* under the marker, opens quicker, and has flexible styling.
* #param {GLatLng} latlng Point to place bar at
* #param {Map} map The map on which to display this InfoBox.
* #param {Object} opts Passes configuration options - content,
* offsetVertical, offsetHorizontal, className, height, width
*/
function InfoBox(opts) {
google.maps.OverlayView.call(this);
this.latlng_ = opts.latlng;
this.map_ = opts.map;
this.offsetVertical_ = -195;
this.offsetHorizontal_ = 0;
this.height_ = 165;
this.width_ = 266;
var me = this;
this.boundsChangedListener_ =
google.maps.event.addListener(this.map_, "bounds_changed", function() {
return me.panMap.apply(me);
});
// Once the properties of this OverlayView are initialized, set its map so
// that we can display it. This will trigger calls to panes_changed and
// draw.
this.setMap(this.map_);
}
/* InfoBox extends GOverlay class from the Google Maps API
*/
InfoBox.prototype = new google.maps.OverlayView();
/* Creates the DIV representing this InfoBox
*/
InfoBox.prototype.remove = function() {
if (this.div_) {
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
}
};
/* Redraw the Bar based on the current projection and zoom level
*/
InfoBox.prototype.draw = function() {
// Creates the element if it doesn't exist already.
this.createElement();
if (!this.div_) return;
// Calculate the DIV coordinates of two opposite corners of our bounds to
// get the size and position of our Bar
var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
if (!pixPosition) return;
// Now position our DIV based on the DIV coordinates of our bounds
this.div_.style.width = "389px";
this.div_.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
this.div_.style.height = "135px";
this.div_.style.top = (pixPosition.y + this.offsetVertical_) + "px";
this.div_.style.display = 'block';
};
/* Creates the DIV representing this InfoBox in the floatPane. If the panes
* object, retrieved by calling getPanes, is null, remove the element from the
* DOM. If the div exists, but its parent is not the floatPane, move the div
* to the new pane.
* Called from within draw. Alternatively, this can be called specifically on
* a panes_changed event.
*/
InfoBox.prototype.createElement = function() {
var panes = this.getPanes();
var div = this.div_;
if (!div) {
// This does not handle changing panes. You can set the map to be null and
// then reset the map to move the div.
div = this.div_ = document.createElement("div");
div.style.border = "1px solid #CCC";
div.style.position = "absolute";
div.style.background = "url('img/proj_box.png')"; //389 × 135
div.style.width = "389px";
div.style.height = "135px";
var contentDiv = document.createElement("div");
contentDiv.style.padding = "30px"
contentDiv.innerHTML = "<b>Hello World!</b>";
var topDiv = document.createElement("div");
topDiv.style.textAlign = "right";
var closeImg = document.createElement("img");
closeImg.style.width = "32px";
closeImg.style.height = "32px";
closeImg.style.cursor = "pointer";
closeImg.src = "http://gmaps-samples.googlecode.com/svn/trunk/images/closebigger.gif";
topDiv.appendChild(closeImg);
function removeInfoBox(ib) {
return function() {
ib.setMap(null);
};
}
google.maps.event.addDomListener(closeImg, 'click', removeInfoBox(this));
div.appendChild(topDiv);
div.appendChild(contentDiv);
div.style.display = 'none';
panes.floatPane.appendChild(div);
this.panMap();
} else if (div.parentNode != panes.floatPane) {
// The panes have changed. Move the div.
div.parentNode.removeChild(div);
panes.floatPane.appendChild(div);
} else {
// The panes have not changed, so no need to create or move the div.
}
}
/* Pan the map to fit the InfoBox.
*/
InfoBox.prototype.panMap = function() {
// if we go beyond map, pan map
var map = this.map_;
var bounds = map.getBounds();
if (!bounds) return;
// The position of the infowindow
var position = this.latlng_;
// The dimension of the infowindow
var iwWidth = this.width_;
var iwHeight = this.height_;
// The offset position of the infowindow
var iwOffsetX = this.offsetHorizontal_;
var iwOffsetY = this.offsetVertical_;
// Padding on the infowindow
var padX = 40;
var padY = 40;
// The degrees per pixel
var mapDiv = map.getDiv();
var mapWidth = mapDiv.offsetWidth;
var mapHeight = mapDiv.offsetHeight;
var boundsSpan = bounds.toSpan();
var longSpan = boundsSpan.lng();
var latSpan = boundsSpan.lat();
var degPixelX = longSpan / mapWidth;
var degPixelY = latSpan / mapHeight;
// The bounds of the map
var mapWestLng = bounds.getSouthWest().lng();
var mapEastLng = bounds.getNorthEast().lng();
var mapNorthLat = bounds.getNorthEast().lat();
var mapSouthLat = bounds.getSouthWest().lat();
// Remove the listener after panning is complete.
google.maps.event.removeListener(this.boundsChangedListener_);
this.boundsChangedListener_ = null;
};
function initialize() {
var myOptions = {
zoom: 11,
//scrollwheel: false,
//navigationControl: false,
//mapTypeControl: false,
//scaleControl: false,
//draggable: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Try W3C Geolocation (Preferred)
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
map.setCenter(initialLocation);
}
, function() {
handleNoGeolocation(browserSupportFlag);
});
// Try Google Gears Geolocation
//AJOUT MARKERS
// Create a base icon for all of our markers that specifies the
var myLatlng = new google.maps.LatLng(48,2);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
google.maps.event.addListener(marker, "click", function(e) {
var infoBox = new InfoBox({latlng: marker.getPosition(), map: map});
});
google.maps.event.trigger(marker, "click");
var myLatlng2 = new google.maps.LatLng(48.8,2);
var marker2 = new google.maps.Marker({
position: myLatlng2,
map: map,
title:"Hello World!"
});
google.maps.event.addListener(marker2, "click", function(e) {
var infoBox2 = new InfoBox({latlng: marker2.getPosition(), map: map});
});
google.maps.event.trigger(marker2, "click");
}
}
</script>
Remove these lines from your initialize function:
google.maps.event.trigger(marker, "click");
google.maps.event.trigger(marker2, "click");