Home > ASP.Net > Get Client Location (Latitude and Longitude) using Google Maps API and Save to Database

Get Client Location (Latitude and Longitude) using Google Maps API and Save to Database

Hi all,

try this example to Get Client Location (Latitude and Longitude) using Google Maps API and save to database

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">
<script src="http://maps.google.com/maps/api/js?sensor=false">
<script type="text/javascript">
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("<%=hfLat.ClientID %>").value = latitude;
document.getElementById("<%=hfLon.ClientID %>").value = longitude;
var coords = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 15,
center: coords,
mapTypeControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map(
document.getElementById("mapContainer"), mapOptions
var marker = new google.maps.Marker({
position: coords,
map: map,
title: "Your current location!"

} else {
alert("Geolocation API is not supported in your browser.");
<style type="text/css">
height: 500px;
width: 800px;
border: 10px solid #eaeaea;
<div id="mapContainer">
<asp:HiddenField ID="hfLat" runat="server" />
<asp:HiddenField ID="hfLon" runat="server" />
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />

In Code behind :

protected void btnSave_Click(object sender, EventArgs e)
string latitude = hfLat.Value;
string longitude = hfLon.Value;

//Save to database here

Hope this helps

Good Luck.

Categories: ASP.Net
  1. October 30, 2016 at 8:29 pm

    It’s very straightforward tto find out any topic on web as
    compared to textbooks, as I found this article at this site.

  2. October 31, 2016 at 1:03 pm

    What’s up everyone, it’s my first visit at this web page, and post is genuinely
    fruitful for me, keep up posting these types of posts.

  3. October 31, 2016 at 8:32 pm

    Nice blog here! Also your web site loads up very fast!

    What web host are you using? Can I get your affiliate
    link to your host? I wish my website loaded up as quickly as yours lol

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: