1

I'm trying to do geolocation using javascript. I've been searching but i can only find examples using maps. I just want to show user's address in a label, textbox or any plain text widget.

Daniel Uribe
  • 778
  • 4
  • 17
Greg E. H.
  • 57
  • 4
  • 3
    Why dont you use html 5's new geolocation feature? – Goaler444 Jan 10 '13 at 18:53
  • I'm start thinking that it would be more helpful to show address instead of position – Greg E. H. Jan 10 '13 at 19:02
  • For address, you will have to use an external service, as far as I know. For example, you can get the country from the geo position by using GeoNames. I have asked a question once how to achieve this: http://stackoverflow.com/questions/13370421/how-to-use-geonames-web-services-using-php However, it makes use of php and not javascript – Goaler444 Jan 10 '13 at 19:08

5 Answers5

3

Check out this example:

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}

function showPosition(position) {
    alert(position.coords.latitude + ", " + position.coords.longitude;
}
Lukas
  • 9,765
  • 2
  • 37
  • 45
3
<script src="http://j.maxmind.com/app/geoip.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" >
window.onload=getGeo;

function getGeo(){
    if (navigator && navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(geoOK, geoKO);
    } else {
        geoMaxmind();
    }
}

function geoOK(position) {
     showLatLong(position.coords.latitude, position.coords.longitude);
}
function geoMaxmind() {
     showLatLong(geoip_latitude(), geoip_longitude());
}

function geoKO(err) {
if (err.code == 1) {
error('El usuario ha denegado el permiso para obtener informacion de ubicacion.');
} else if (err.code == 2) {
error('Tu ubicacion no se puede determinar.');
} else if (err.code == 3) {
error('TimeOut.')
} else {
error('No sabemos que pasó pero ocurrio un error.');
}
}

function showLatLong(lat, longi) {
var geocoder = new google.maps.Geocoder();
var yourLocation = new google.maps.LatLng(lat, longi);
geocoder.geocode({ 'latLng': yourLocation },processGeocoder);

}
function processGeocoder(results, status){

if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
document.forms[0].dir.value=results[0].formatted_address;
document.getElementById("")
} else {
error('Google no retorno resultado alguno.');
}
} else {
error("Geocoding fallo debido a : " + status);
}
}
function error(msg) {
alert(msg);
}
</script>

<form align="left">
        <input type="text" name="dir" />
</form>
Daniel Uribe
  • 778
  • 4
  • 17
1

Check out the HTML5 Geolocation API: http://diveintohtml5.info/geolocation.html

It's as simple as:

navigator.geolocation.getCurrentPosition(function(location) {
    // This function is called if the location is found. Use location.coords.latitude & location.coords.longitude
}, function() {
    // This function is called if the location is not found.
});

It's not supported by all browsers though. If you're making this public, I'd also recommend using Modernizr to detect support: http://modernizr.com

0

You can use geoplugin.

Add the following to your <head> tag.

<script  src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>

This will determine the location based on your IP address.

Inside your javascript, you can call

latitude = geoplugin_latitude();
longitude = geoplugin_longitude();
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
0

Try this:

 <script src="http://www.merkwelt.com/people/stan/geo_js/js/geo.js?id=1" type="text/javascript" charset="utf-8"></script>
     <script type="text/javascript">
         if (geo_position_js.init()) {
             geo_position_js.getCurrentPosition(success_callback, error_callback, { enableHighAccuracy: true });
         }
         else {
             alert("Functionality not available");
         }

         function success_callback(p) {
             //alert('lat=' + p.coords.latitude.toFixed(2) + ';lon=' + p.coords.longitude.toFixed(2));
             longitude = p.coords.longitude.toFixed(2);
             latitude = p.coords.latitude.toFixed(2);
         }

         function error_callback(p) {
             alert('error=' + p.code);
         }
</script>

Then from the longitude and latitude, you can get the location using google api: http://maps.googleapis.com/maps/api/geocode/json?latlng=33.84,35.52&sensor=false

Alex
  • 5,971
  • 11
  • 42
  • 80