Showing Location Using Google Maps

Dotnet4ashu
Posted by Dotnet4ashu under ASP.NET category on | Points: 40 | Views : 1623
Hello All Experts I found a new way to find a location which might help you

and you may save these latitude and longitudes in your datab ase

Code Searching location and submit its lat and longg in your database....................


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>

<script src=" http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAUvgMDpSwpIVSdk4Lt1gyZxRrPG9ukeNb8tYptMFxTfI6RCHRlBR6oN-gOMyEFzILA_3i60HC7gO7HQ "
type="text/javascript">
//My Google Maps Key
</script>

<script type="text/javascript">

function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl (new GSmallMapControl());
map.addControl(new GMapTypeControl());
var center = new GLatLng(-22.98699983834975, -43.210344314575195);
map.setCenter(center, 11);
map.setMapType(G_SATELLITE_MAP);
geocoder = new GClientGeocoder();

var marker = new GMarker(center, {draggable: true});
map.addOverlay(marker);
document.getElementById("lat").value = center.lat();
document.getElementById("lng").value = center.lng ();
geocoder = new GClientGeocoder();

GEvent.addListener(marker, "dragend", function() {
var point =marker.getPoint();
map.panTo(point);
document.getElementById("lat").value = point.lat();
document.getElementById("lng").value = point.lng();
});

GEvent.addListener(map, "moveend", function() {
map.clearOverlays();
var center = map.getCenter();
var marker = new GMarker(center, {draggable: true});
map.addOverlay(marker);
document.getElementById ("lat").value = center.lat();
document.getElementById("lng").value = center.lng();

GEvent.addListener(marker, "dragend", function() {
var point =marker.getPoint();
map.panTo(point);
document.getElementById("lat").value = point.lat();
document.getElementById("lng").value = point.lng();
});
});
}
}

function showAddress(address) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
if (geocoder) {
geocoder.getLatLng (
address,
function(point) {
if (!point) {
alert(address + " city not found !");
}
else {
document.getElementById("lat").value = point.lat();
document.getElementById("lng").value = point.lng();
map.clearOverlays()
map.setCenter(point, 14);
var marker = new GMarker(point, {draggable: true});
map.addOverlay(marker);

GEvent.addListener(marker, "dragend", function() {
var pt =marker.getPoint();
map.panTo(pt);
document.getElementById("lat").value = pt.lat();
document.getElementById("lng").value = pt.lng();
});

GEvent.addListener(map, "moveend", function() {
map.clearOverlays();
var center = map.getCenter();
var marker = new GMarker(center, {draggable: true});
map.addOverlay(marker);
document.getElementById ("lat").value = center.lat();
document.getElementById("lng").value = center.lng();

GEvent.addListener(marker, "dragend", function() {
var pt =marker.getPoint();
map.panTo(pt);
document.getElementById("lat").value = pt.lat();
document.getElementById("lng").value = pt.lng();
});
});
}}
);
}}
</script>

</head>
<body onload="load()" onunload="GUnload()">
<form id="form1" runat="server">

<script type="text/javascript" language="JavaScript">
<!--
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all )) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers)
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")
// -->
</script>

<div>
<b>Find coordinates by moving around the map</b>
<br>
<br>
a. Drag and drop the map to each location.
<br>
b. Zoom in for better accuracy.
<br>
c. Drag and drop the marker to pinpoint the place. The coordinates are refreshed
at the end of each movement.
<br>
<br>
<b>Type city name:</b>
<input type="text" size="34" name="address" value="BagBazar" />
<input type="button" value="Search!" id="bt" onclick="showAddress(document.form1.address.value);" />
<br>
Current coordinates:
<table bgcolor="#FFFFFF" width="300">
<tr>
<td width="70">
<b>Latitude:</b>
</td>
<td>
<input type="text" size="34" name="Logit" value="" id="lat" runat="server" />
</td>
</tr>
<tr>
<td width="70">
<b>Longitude:</b>
</td>
<td>
<input type="text" size="34" value="" id="lng" runat="server" />
</td>
</tr>
</table>
<br>
<div align="center" id="map" style="width: 900px; height: 660px">
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>


On CS Page

public partial class ali : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Visible = true;
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
cmd = new SqlCommand("insert into t1 (lat,longitude)values(@lat,@longitude) ", con);
cmd.Parameters.AddWithValue("@lat",lat.Value.ToString());
cmd.Parameters.AddWithValue("@longitude", lng.Value.ToString());
cmd.ExecuteNonQuery();
con.Close();
ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert(Data Inserted Successfully)", true);

}

}
catch (Exception ex)
{
ex.ToString();
}

}

}

Comments or Responses

Login to post response