0

I have this javascript script, and I would like to print latitude and longitude obtained through geolocation in the same html page where the script is added. How can I do that?

Edit : I specified my question

eng_mazzy
  • 1,049
  • 4
  • 23
  • 39

3 Answers3

2

I think you are asking how to display latitude and longitude that you already have, rather than actually obtaining latitude and longitude. If this is the case, you can do this:

<dl>
   <dt>Latitude</dt>
   <dd id="latitude"></dd>
   <dt>Longitude</dt>
   <dd id="longitude"></dd>
</dl>
<script type="text/javascript">
    document.getElementById("latitude").innerHTML = "lat value";
    document.getElementById("longitude").innerHTML = "long value";
</script>

If this is not the case, you question should probably be "How do I obtain the current latitude and longitude of the website visitor?".

Fenton
  • 241,084
  • 71
  • 387
  • 401
1

Here is the jsfiddle that will do what you need.

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
-1


I don't think its possible to get the location on a regular computer since most of those don't have a GPS.

If you are speaking about getting the location using Javascript on a mobile device, that makes a bit more sense . There are several libraries available such as geo-location-javascript on google-code. Also this article isn't bad : http://www.greenhughes.com/content/where-are-you-find-out-geolocation-javascript

Usually when services get your estimated location in a browser, without having a GPS on the device, they do it by analyzing your IP address infront of a database that has the locations of all IP addresses in the world. But again, that is only an estimate and usually not "accurate" as an actual GPS. MaxMind are probably the largest company offering this service.

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83