Hi ,
Firstly i suggest if you have limited locations then go and create table which will hold following things. you can modify columns depending on your need.
public class Location
{
public int Id { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string LocationName { get; set; }
public string CompleteGeoLocation { get; set; }
}
Add below code in page_load method
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Location> locationCollection = new List<Location> {
new Location {Id=1,Latitude=18.31,Longitude=73.71,LocationName="Pune",CompleteGeoLocation="18.31,73.71"},
new Location {Id=2,Latitude=51.30,Longitude=0.7,LocationName="London",CompleteGeoLocation="51.30,0.7"},
new Location {Id=3,Latitude=35.41,Longitude=139.41,LocationName="Tokyo",CompleteGeoLocation="35.41,139.41"},
new Location {Id=4,Latitude=42.36,Longitude=83.8,LocationName="Troy",CompleteGeoLocation="42.36,83.8"},
};
ddlLocation.DataSource = locationCollection;
ddlLocation.DataValueField = "CompleteGeoLocation";
ddlLocation.DataTextField = "LocationName";
ddlLocation.DataBind();
}
}
Below is the aspx code. Please add your own apiKey
<html>
<head id="Head1" runat="server">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#mapcanvas { height: 50% }
</style>
<script type="text/javascript">
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD-0MJH3LspBYtFwfnWT7icqWzRs3bsds_uE4&sensor=true">
</script>
<script type="text/javascript">
function initialize() {
if (document.getElementById("ddlLocation").selectedIndex != -1)
{
var data = document.getElementById("ddlLocation").value.split(',');
var Latitude = parseFloat(data[0]);
var Longitude = parseFloat(data[1]);
var mapOptions = {
center: new google.maps.LatLng(Latitude, Longitude),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcanvas"),
mapOptions);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<form id="form1" style="height:750px" runat="server">
<div id="mapcanvas" style="width:50%" runat="server" clientidmode="Static" />
<div>
<asp:dropdownlist id="ddlLocation" runat="server" width="200px" autopostback="true" xmlns:asp="#unknown">
Enabled="True" ></asp:dropdownlist>
</div>
</form>
</body>
</html>
after this on first load html will dislpay first location from location drop down.
later you can change location on selection dropdown location change.