Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7917567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:11:40+00:00 2026-06-03T15:11:40+00:00

How can I save lat and lng values to variables using HTML5 geolocation API?

  • 0

How can I save lat and lng values to variables using HTML5 geolocation API?

This is my code, copied from w3schools. How can I save coordinates to variables like var x= position.coords.latitude; and var y= position.coords.longitude; instead of just showing the values like the code is doing right now? I am beginner with javascript so I don’t know how to do this

 <!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>

var x=document.getElementById("demo");
window.onload = function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.watchPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br />Longitude: " + position.coords.longitude;  
  }
</script>
</body>
</html>

And in my application I need to send values of those variables to server every minute. Would it be better to do the geolocation with watchposition or execute function that does getcurrentposition every minute?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:11:41+00:00Added an answer on June 3, 2026 at 3:11 pm

    Adding them to variables is as easy as:

    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    

    OR as an object:

    var coords = {lat: "", lon: ""};
    

    Then to use the object in your code:

    function showPosition(position)
    {
       coords.lat = position.coords.latitude;
       coords.lon = position.coords.longitude;
    
       x.innerHTML="Latitude: " + coords.lat + 
       "<br />Longitude: " + coords.lon;  
    }
    

    As for sending the variables to a server this depends on what your serverside technology is but you will find many examples on google.

    function sendToServer(){
       // here you can reuse the object to send to a server
       console.log("lat: " + coords.lat);
       console.log("lon: " + coords.lon);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a line which can be edited using this code: var line_points =
is there a way that I can save the model by using dictionary for
I'm having a bit of trouble with knowing how I can save items from
So, I'm building an application which save locations to a database using lat/lon coordinates.
You can save the code below and try it out. In firefox ,it's full
I know i can save strings in res/values/strings.xml but if someone disassembles the dex
Internet Explorer can save its content as an MHTML file using the Save As...
Possible Duplicate: How can I save and load the alpha values of a UIButton
I have the following code where i save the xml file into this particular
You can save a SQL Server 2000 DTS package as a VB .BAS file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.