0

Hi guys I try to add the latitude and longitude from my current location to an input.

Do you have any idea of what to do? this is my code

<input type="text" name="demo" id="demo" value="<?php echo $demo ;?>" />
<script>
document.getElementById("demo").value=latitude;
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
    else{
        alert("Geolocation is not supported by this browser.");
    }
  }
function showPosition(position)
  {   
 latitude.innerHTML= position.coords.latitude; 
 longitude.innerHTML=position.coords.longitude;
  }

</script>
rpax
  • 4,468
  • 7
  • 33
  • 57
  • @Teemu: with respect to return value the parameter is a function that assigns stuff. – Chris Mar 21 '14 at 17:07
  • I have a button to call the function getLocation(); – Domna Michael Mar 21 '14 at 17:09
  • what is latitude? at the beginning of your script you are setting the input value to `latitude` which leads me to believe it should be text of some kind but later on in the `showPosition` function you seem to be setting its inner HTML suggesting it is a DOM element. Perhaps you should be more precise about what you think this should be and indeed more precise about what problem you are having exactly. – Chris Mar 21 '14 at 17:09
  • possible duplicate of [Get Latitude/Longitude Button to Fill Input Boxes](http://stackoverflow.com/questions/9067389/get-latitude-longitude-button-to-fill-input-boxes) – Chris Mar 21 '14 at 17:10
  • Additionally when this seems to be a duplicate of the very first item in the "Related" questions list on the side it suggests you haven't done much to actually research what you are doing yourself. – Chris Mar 21 '14 at 17:11

1 Answers1

0

Probably I did not understand, but if your intention is to add the coordinates to the text field try this

<body>
<script>
function getLocation(){
  if (navigator.geolocation){
     navigator.geolocation.getCurrentPosition(showPosition);
    }else{
       alert("Geolocation is not supported by this browser.");
    }
  }
 getLocation()
function showPosition(position) {
      var str="Longitude:"+ position.coords.longitude+"Latitude:"+ position.coords.latitude
      document.getElementById("demo").value=str
  }
</script>
<input type="text" name="demo" id="demo" value="" />
</body>
Devima
  • 1,566
  • 2
  • 10
  • 16