Geolocation - how to get city from long and lat

coder2222

I am trying to get the city out of Geolocation API. I have just started some JS and jQuery learning so it might be a basic mistake.

$(document).ready(function(){
    function displayLocation(latitude,longitude){
        var request = new XMLHttpRequest();

        var method = 'GET';
        var url = 
            'http://maps.googleapis.com/maps/api/geocode/json?latlng=' 
            + latitude + ',' + longitude + '&sensor=true';
        var async = true;

        request.open(method, url, async);
        request.onreadystatechange = function(){
            if(request.readyState == 4 && request.status == 200){
                var data = JSON.parse(request.responseText);
                var address = data.results[0];
                alert(address.city.short_name);
            }
        };
        request.send();
    };

    var successCallback = function(position){
        var x = position.coords.latitude;
        var y = position.coords.longitude;
        displayLocation(x,y);
    };

    navigator.geolocation.getCurrentPosition(successCallback);
});
The Lost Cookie Session

This should help

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        function displayLocation(latitude,longitude){
        var request = new XMLHttpRequest();

       var method = 'GET';
       var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
       var async = true;

       request.open(method, url, async);
       request.onreadystatechange = function(){
       if(request.readyState == 4 && request.status == 200){
         var data = JSON.parse(request.responseText);
         alert(request.responseText); // check under which type your city is stored, later comment this line
         var addressComponents = data.results[0].address_components;
         for(i=0;i<addressComponents.length;i++){
            var types = addressComponents[i].types
            //alert(types);
            if(types=="locality,political"){
               alert(addressComponents[i].long_name); // this should be your city, depending on where you are
             }
           }
        //alert(address.city.short_name);
       }
    };
   request.send();
 };

 var successCallback = function(position){
 var x = position.coords.latitude;
 var y = position.coords.longitude;
 displayLocation(x,y);
  };


 navigator.geolocation.getCurrentPosition(successCallback);

  });
 </script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check if lat long is in the city limits

From Dev

HTML5 geolocation: How to get pass lat and long values to an AJAX call after a success or error callback

From Dev

How can I get city name without current location(lat-long) in iOS?

From Dev

How to get lat and lon geolocation coordinates?

From Dev

Geolocation closest location(lat, long) from my position

From Dev

I want to get the lat-long of a city name

From Dev

I want to get the lat-long of a city name

From Dev

How to check if latitude and longitude of a specific address belongs to lat and long of a city?

From Dev

How to check if latitude and longitude of a specific address belongs to lat and long of a city?

From Dev

facebook api how to get lat/long from user_location

From Dev

How to get PlaceID from name, or lat, long in Android?

From Dev

How to get lat and long from sdo_geometry in oracle

From Dev

How to get distance from long/lat for multiple samples in a dataset in r

From Dev

How to get the long/lat information from a NodeID and vice versa

From Dev

How to get distance from long/lat for multiple samples in a dataset in r

From Dev

How to do reverse geocoding with Cordova Geolocation API to get City and Country?

From Dev

Get numbers from array(Lat Long ) in PHP

From Dev

Get numbers from array(Lat Long ) in PHP

From Dev

PHP Geolocation (Street Address -> Lat-Long)

From Dev

How to get user current location with lat and long

From Dev

OSM - Extract Street Name, Lat/Long, and City

From Dev

OSM - Extract Street Name, Lat/Long, and City

From Dev

Geolocation, Accuracy and Affine Transformation: What is causing my inaccurate conversion from a lat/long location into a point on my image

From Dev

How to get city from coordinates?

From Dev

How do I get lat / long from google geocode API in python 3?

From Dev

how to get lat long from google map when our own geojson layer is there?

From Dev

How to Swap Coordinates of jts.geom.Geometry object from Lat, Long to Long,Lat in JTS

From Dev

How to get UTC time offsets from a geolocation

From Dev

How to get UTC time offsets from a geolocation

Related Related

  1. 1

    How to check if lat long is in the city limits

  2. 2

    HTML5 geolocation: How to get pass lat and long values to an AJAX call after a success or error callback

  3. 3

    How can I get city name without current location(lat-long) in iOS?

  4. 4

    How to get lat and lon geolocation coordinates?

  5. 5

    Geolocation closest location(lat, long) from my position

  6. 6

    I want to get the lat-long of a city name

  7. 7

    I want to get the lat-long of a city name

  8. 8

    How to check if latitude and longitude of a specific address belongs to lat and long of a city?

  9. 9

    How to check if latitude and longitude of a specific address belongs to lat and long of a city?

  10. 10

    facebook api how to get lat/long from user_location

  11. 11

    How to get PlaceID from name, or lat, long in Android?

  12. 12

    How to get lat and long from sdo_geometry in oracle

  13. 13

    How to get distance from long/lat for multiple samples in a dataset in r

  14. 14

    How to get the long/lat information from a NodeID and vice versa

  15. 15

    How to get distance from long/lat for multiple samples in a dataset in r

  16. 16

    How to do reverse geocoding with Cordova Geolocation API to get City and Country?

  17. 17

    Get numbers from array(Lat Long ) in PHP

  18. 18

    Get numbers from array(Lat Long ) in PHP

  19. 19

    PHP Geolocation (Street Address -> Lat-Long)

  20. 20

    How to get user current location with lat and long

  21. 21

    OSM - Extract Street Name, Lat/Long, and City

  22. 22

    OSM - Extract Street Name, Lat/Long, and City

  23. 23

    Geolocation, Accuracy and Affine Transformation: What is causing my inaccurate conversion from a lat/long location into a point on my image

  24. 24

    How to get city from coordinates?

  25. 25

    How do I get lat / long from google geocode API in python 3?

  26. 26

    how to get lat long from google map when our own geojson layer is there?

  27. 27

    How to Swap Coordinates of jts.geom.Geometry object from Lat, Long to Long,Lat in JTS

  28. 28

    How to get UTC time offsets from a geolocation

  29. 29

    How to get UTC time offsets from a geolocation

HotTag

Archive