In this article I will explain with an example, how to find the Address of a location using Geographical coordinates i.e. Latitude and Longitude using the Google Maps Geocoding API in ASP.Net.
HTML Markup
The following HTML markup consists of:
TextBox – For entering Geographical coordinates i.e. Latitude and Longitude.
Button – For displaying Address of location.
The Button has been assigned with
JavaScript OnClientClick event handler.
Latitude:
<asp:TextBox ID="txtLatitude" runat="server" Text="18.92488028662047" />
Longitude:
<asp:TextBox ID="txtLongitude" runat="server" Text="72.8232192993164" />
<asp:Button runat="server" Text="Get Address" OnClientClick="return GetAddress()" />
Getting Address of location using Geographical coordinates using JavaScript
When the Button is clicked, the
GetAddress JavaScript function is called.
Inside the
GetAddress JavaScript function, the values of Latitude and Longitude are fetched from respective TextBoxes.
Then, using the Latitude and Longitude values the Google Maps Geocoding API LatLng object is created.
Next, the geocode function of the Geocoder object is called and the LatLng object is passed as parameter.
Finally, if
GeocoderStatus is OK then the
formatted_address of the location is displayed using
JavaScript Alert Message Box.
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<API_KEY>"></script>
<script type="text/javascript">
function GetAddress() {
var lat = parseFloat(document.getElementById('<%=txtLatitude.ClientID%>').value);
var lng = parseFloat(document.getElementById('<%=txtLongitude.ClientID%>').value);
var latlng = new google.maps.LatLng(lat, lng);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
alert("Location: " + results[1].formatted_address);
}
}
});
return false;
}
</script>
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Downloads