javascript:

var map;
var geocoder;
var bounds;

var _count = 0;
var _max = 0;

var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;

function InitializeTimer() {
secs = 100;
StopTheClock();
StartTheTimer();
}

function StopTheClock() {
if (timerRunning)
clearTimeout(timerID);
timerRunning = false;
}

function StartTheTimer() {
if (secs == 0) {
StopTheClock();
}
else {
if (_max == _count) {
SetZoom();
StopTheClock();
}
else {
self.status = secs;
secs = secs – 1;
timerRunning = true;
timerID = self.setTimeout(“StartTheTimer()”, delay);
}
}
}

function SetZoom() {
map.setZoom(map.getBoundsZoomLevel(bounds));
map.setCenter(bounds.getCenter());
}

function initializeGoogleMap(address, control) {
if (GBrowserIsCompatible()) {
bounds = new GLatLngBounds;
geocoder = new GClientGeocoder();

map = new GMap2(document.getElementById(control));

map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

map.setCenter(new GLatLng(0, 0), 0);

var addresses = address.split(“;”);

_count = 0;
_max = addresses.length;

for (var i = 0; i < addresses.length; i++) {
AddAddress(addresses[i]);
}

InitializeTimer();
}
}

function AddAddress(address) {
var data = address.split(“~”);
geocoder.getLatLng(
data[1],
function (point) {
if (!point) {
_count++;
}
else {
var marker = new GMarker(point);
map.addOverlay(marker);
bounds.extend(marker.getPoint());

_count++;
var html = “<b>” + data[0] + “</b><br />” + data[1] + “<br />Phone: ” + data[2] + “, Fax: ” + data[3];
GEvent.addListener(marker, ‘click’, function () {
marker.openInfoWindowHtml(html);
});
}
}
);
}

Markup:
<asp:Literal ID=”litMapScript” runat=”server”></asp:Literal>
<div>
<div id=”map” runat=”server” style=”width: 570px; height: 290px;”>
</div>
</div>

C# :
string locations = “”;
list<Location> allLocation = all locations;
foreach (Location l in allLocation)
{
if (locations == “”)
locations += l.Title + “~”+ l.Address + “~” + l.Phone + “~” + l.Fax;
else
locations += “;” + l.Title + “~” + l.Address + “~” + l.Phone + “~” + l.Fax;

}

litMapScript.Text = string.Format(@”<script src=’http://maps.google.com/maps?file=api&amp;v=2&amp;key={0}&sensor=false’
type=’text/javascript’></script>”, ConfigurationManager.AppSettings[“MAPKEY”]);
Page.ClientScript.RegisterStartupScript(this.GetType(), “javaScript”, string.Format(“initializeGoogleMap(‘{0}’,'{1}’);”, locations, map.ClientID), true);

ConfigurationManager.AppSettings[“MAPKEY”] -> google map key

Advertisements