Introduction
In this article, I will describe how to get your geolocation (Latitude
and Longitude) using Google maps in HTML5.
Actually The HTML5 Geolocation API is used to get the geographical position of a
user.
Now here the browser provides support for doing this Geolocation features.
I.E 9, Firefox, Chrome, Safari and Opera support Geolocation.
In HTML5 the getCurrentPosition() method is to get the user's position.
Here we will get the first user latitude and longitude by using the
getCurrentPosition() method.
Thereafter with this latitude and longitude, we will feed the data to the Google
maps and will get the current user locations in maps.
Now here I am showing the steps for using these useful HTML5 features in ASP.NET.
Step 1:
Create a New web application project in ASP.NET.
Step 2:
Add a new webpage named "geolocation.aspx" and set it as the startup web
page.
Step 3:
Now in the source page first write the following code:
Descriptions



- position.coords.latitude and position.coords.longitude;
And finally, display it in the "mapholder" div.
So here is the full code segment:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="geolocation.aspx.cs" Inherits="WebApplication1.geolocation" %>
- <!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></title>
- </head>
- <body >
- <p id="demo">Click the button to get your position:</p>
- <button onclick="getLocation()">Get your Location</button>
- <div id="mapholder" ></div>
- <script type="text/javascript">
- var x = document.getElementById("demo");
- function getLocation() {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(showPosition, showError);
- }
- else { x.innerHTML = "Geolocation is not supported by this browser."; }
- }
- function showPosition(position) {
- var latlondata = position.coords.latitude + "," +position.coords.longitude;
- var latlon = "Your Latitude Position is:=" + position.coords.latitude + "," + "Your Longitude Position is:=" +position.coords.longitude;
- alert(latlon)
- var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="
- + latlondata + "&zoom=14&size=400x300&sensor=false";
- document.getElementById("mapholder").innerHTML = "
- <img src='" + img_url + "' />";
- }
- function showError(error) {
- if (error.code == 1) {
- x.innerHTML = "User denied the request for Geolocation."
- }
- else if (err.code == 2) {
- x.innerHTML = "Location information is unavailable."
- }
- else if (err.code == 3) {
- x.innerHTML = "The request to get user location timed out."
- }
- else {
- x.innerHTML = "An unknown error occurred."
- }
- }
- </script>
- <form id="form1" runat="server">
- <div></div>
- </form>
- </body>
- </html>
Now when you run the program and press the button, first it will look like
the following picture;
it will give you latitude and longitude positions.



Luai KayyaliPosted Jun 18, 2022, 1:23 PM
Hello, it worked for me, you need https
Abdul Amin KhanPosted Jun 28, 2020, 2:35 PM
It is not asking permission without permission showing location please suggest me how to add permission before getting location
Anupam ModakPosted Sep 27, 2018, 10:34 PM
Can this code give me location even if the web application is not HTTPS?
ekwugha franklinPosted Jul 4, 2018, 10:23 AM
Not orking ro
Nagendra KushwahPosted Jan 19, 2018, 2:59 AM
Map is not showing after popup message. How can I solve it?
solomon sojirinPosted Apr 24, 2017, 8:16 AM
Written the codes and i am running it its not bringing out the google map
Manav PandyaPosted Nov 29, 2016, 8:31 AM
How can we do same in asp.net mvc sir ???
kalu singh raoPosted Jul 4, 2016, 2:16 AM
Nice...
Prasad BhagatPosted Dec 22, 2015, 7:20 AM
can u bind the longitude and latitude values to asp.net textboxes .if yes how can? or no why?
salim RezaPosted Sep 8, 2012, 2:19 PM
thx brother , but how i can keep the location in object ? pls answer my question
salim RezaPosted Sep 8, 2012, 2:18 PM
thx brother , but how i can keep the location in object ? pls answer my question
Stelia JacksonPosted Jan 19, 2012, 11:12 PM
thnx for sharing ur views...
Sanjoli GuptaPosted Jan 19, 2012, 3:28 AM
thnx for sharing...
Abhi KumarPosted Jan 18, 2012, 11:37 PM
Very helpful article.
Sonakshi SinghPosted Jan 18, 2012, 11:11 PM
Good approach Shirsendu..
Monika AroraPosted Jan 18, 2012, 10:52 PM
You have explained your article very nicely. Thanks for sharing.
Aaron CronjePosted Jan 18, 2012, 10:47 PM
Good Explain......