0

hi everybody I hope you are well,

here is my code:

var x = document.getElementById('geo_output');
//x.innerHTML = "here is geo_output";   	
function getLocation()
{
  if(navigator.geolocation)
    {
      //x.innerHTML = "Supporting";
      navigator.geolocation.getCurrentPosition(showPosition);
    }
  else
    {
      x.innerHTML = "Browser not Supporting";
    }
}

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

$('#geo_output').click(getLocation)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<h1>trace your location</h1>	
	<button onClick="getLocation()">Get Location</button>	
  <div id="geo_output">
  </div>
  <br>	<br>	<br>	<br>	<br>	
  <div id="form_output">
    <form action="includes/signup.php" method="POST">
    <input type="text" name="lat" placeholder="latitude">
    <br>
    <input type="text" name="lon" placeholder="longitude">
    <br>
    <input type="text" name="id" placeholder="id">
    <br>
    <input type="text" name="name" placeholder="first_name">
    <br>
    <input type="text" name="address" placeholder="address">
    <br>
    <input type="text" name="description" placeholder="description">
    <br>
    <button type="submit" name="submit"> envoyer </button>
  </div>

when i execute it , it gives me a correct result.

but i want to know how to do that, when i click on the button gate location, the latitude and longitude will be inserted directly and automatically into their <input>

<input type="text" name="lat" placeholder="latitude">
<br>
<input type="text" name="lon" placeholder="longitude">

thank you friends

1
  • 2
    Give them an id in the html, find them with getElementById and set their value property. Commented Mar 7, 2018 at 11:07

2 Answers 2

1

To select the right input, give it an ID:

<input id="latitude" type="text" name="lat" placeholder="latitude">
<br>
<input id="longitude" type="text" name="lon" placeholder="longitude">

And in showPosition function:

document.getElementById('latitude').value = position.coords.latitude;
document.getElementById('longitude').value = position.coords.longitude;
Sign up to request clarification or add additional context in comments.

5 Comments

how we can make it in android ?? have you any information about that sir ??
do you use any tool/framework, like ionic or cordova/phonegap?
no sir , it's just android java , in android studio
In Android there is the LocationListener class which you can you use to get location information. This a related question: stackoverflow.com/q/1513485/4730201
thank you sir ,..sorry sir i wanna ask you again because i'm really curious a lot exactly about the action that when i click on the button , the result will be inserted directly and automatically into their <input> how i can do it in the android??
1

Change the element to have an ID like this

<input type="text" id="lat" id="lat" placeholder="latitude">

Then in JS code do the following

 document.getElementById('lat').value = position.coords.latitude;

And the same for the longitude

2 Comments

sir i wanna ask you again because i'm really curious a lot exactly about the action that when i click on the button , the result will be inserted directly and automatically into their <input> how i can do it in the android??? any ideas or articles who will help me ????
Hi @stackoverstudent, I'm not sure how this will be done in Android. I assume it would have the same effect. I would suggest asking another question and others should answer. Sorry I can't answer this for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.