Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Function - Convert Geolocation Code to Street Address

I am looking for a javascript function or jquery library to convert geolocation code (e.g. 42.2342,32.23452) to street address

For examples.

    navigator.geolocation.getCurrentPosition(
      function(pos) {
        $("#lat_field").val(pos.coords.latitude);
        $("#long_field").val(pos.coords.longitude);
      }
    );

Here is a google api URL to get address data

http://maps.googleapis.com/maps/api/geocode/json?latlng=41.03531125,29.0124264&sensor=false

I want to see "formatted_address" : "Hacı Hesna Hatun Mh., Paşa Limanı Cd 2-26, 34674 Istanbul, Türkiye",

    navigator.geolocation.getCurrentPosition(
      function(pos) {
        $("#lat_field").val(pos.coords.latitude);
        $("#long_field").val(pos.coords.longitude);
        $("#adress_data").getaddrfromlatlong(pos.coords.latitude,pos.coords.longitude)
      }
    );

This function should be how ? ``getaddrfromlatlong()

like image 549
Erhan H. Avatar asked Jan 21 '12 07:01

Erhan H.


People also ask

How do I find a street name from latitude and longitude?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);

How do you reverse geocode?

Reverse geocoding can be carried out systematically by services which process a coordinate similarly to the geocoding process. For example, when a GPS coordinate is entered the street address is interpolated from a range assigned to the road segment in a reference dataset that the point is nearest to.

Which is the best method to use to geocode a static list of address?

Use the Places API Place Autocomplete service to obtain a place ID, then the Geocoding API to geocode the place ID into a latlng.


1 Answers

Try this:

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">     
   var geocoder = new google.maps.Geocoder();
   var latLng = new google.maps.LatLng(41.03531125,29.0124264);

   if (geocoder) {
      geocoder.geocode({ 'latLng': latLng}, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0].formatted_address);
         }
         else {
            console.log("Geocoding failed: " + status);
         }
      });
   }    
</script>
like image 61
Greg Avatar answered Nov 14 '22 21:11

Greg