Lifeline, CLever, and L3D 

<< Back to Working

Google Maps API - Markers and Polylines

The Code:

<head>
<script src
="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAHcNh3mmTQyAZpWp_BdIcjhRW3ahPvXi-1UokraFc4d7K2_KhwRSeChMKIkZTdOZUOygA_Q9_egtSGw" type="text/javascript"></script>
</head>

<body>
<div id=
"map" style="width: 500px; height: 400px"></div>
<script type
="text/javascript">

//<![CDATA[
var map = new GMap(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.centerAndZoom(new GPoint(-122.241944, 37.441944), 4);

// Add 10 random Markers in the map viewport using the default icon
var bounds = map.getBoundsLatLng();
var width = bounds.maxX - bounds.minX;
var height = bounds.maxY - bounds.minY;
for( var i=0; i<10; i++)
{
var point = new GPoint(bounds.minX+width*Math.random(), bounds.minY+height*Math.random());
var marker = new GMarker(point);
map.addOverlay(marker);
}

// Add a polyline with 4 random points. Sort the points by long so that
// the line does not intersect itself
var points = [];
for( var i=0; i<5; i++)
{
points.push(new GPoint(bounds.minX+width*Math.random(), bounds.minY+height*Math.random()));
}
points.sort(function(p1,p2) {return p1.x - p2.x; });
map.addOverlay(new GPolyline(points));
map.openInfoWindow(map.getCenterLatLng(), document.createTextNode("Hello world"));

GEvent.addListener(map, 'click', function(overlay, point)
{
if(overlay) { map.removeOverlay(overlay); }
else if(point) { map.addOverlay(new GMarker(point)); }
});

// window.setTimeout(function() {
//map.recenterOrPanToLatLng(new GPoint(-122.1569, 37.4569)); }, 2000);
//]]>
</script>
</body>