Skip to content

Instantly share code, notes, and snippets.

@vinaydotblog
Last active November 18, 2017 16:34
Show Gist options
  • Save vinaydotblog/34d9b2292e27f0bfee28 to your computer and use it in GitHub Desktop.
Save vinaydotblog/34d9b2292e27f0bfee28 to your computer and use it in GitHub Desktop.

Revisions

  1. Vinay Aggarwal revised this gist Nov 18, 2017. 1 changed file with 4 additions and 9 deletions.
    13 changes: 4 additions & 9 deletions latlng_to_city.js
    Original file line number Diff line number Diff line change
    @@ -17,16 +17,11 @@ function getCity(latlng) {
    .done(function(data){
    var location;
    if( data.results && data.results.length ) {
    var components = data.results[0].address_components;

    for(var i = 0; i < components.length; i++) {
    if( components[i].long_name ) {
    location = components[i].long_name;
    break;
    }
    var components = data.results[0];
    if( components.formatted_address ) {
    location = components.formatted_address;
    return def.resolve(location);
    }

    if(location) return def.resolve(location);
    }

    return def.reject();
  2. Vinay Aggarwal revised this gist Nov 18, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion latlng_to_city.js
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ function getCity(latlng) {
    var components = data.results[0].address_components;

    for(var i = 0; i < components.length; i++) {
    if( ~components[i].types.indexOf('locality') ) {
    if( components[i].long_name ) {
    location = components[i].long_name;
    break;
    }
  3. Vinay Aggarwal revised this gist Nov 18, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions latlng_to_city.js
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@
    */

    function getCity(latlng) {
    var $ = jQuery;
    var def = $.Deferred();

    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latlng)
  4. Vinay Aggarwal revised this gist Feb 21, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion latlng_to_city.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,8 @@
    function getCity(latlng) {
    var def = $.Deferred();

    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latlng).done(function(data){
    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latlng)
    .done(function(data){
    var location;
    if( data.results && data.results.length ) {
    var components = data.results[0].address_components;
  5. Vinay Aggarwal revised this gist Feb 21, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion latlng_to_city.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,8 @@
    * $elem.val( cityName )
    * });
    *
    * Info: jQuery is required for $.Deferred() to work!
    * Info: jQuery is required for $.Deferred() and $.getJSON to work!
    * Regardless of everything modify it as per your convenience :)
    */

    function getCity(latlng) {
  6. Vinay Aggarwal revised this gist Feb 21, 2016. No changes.
  7. Vinay Aggarwal revised this gist Feb 21, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion latlng_to_city.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    function getCity(latlng) {
    var def = $.Deferred();

    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?latlng=12.9715987,77.5945627').done(function(data){
    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latlng).done(function(data){
    var location;
    if( data.results && data.results.length ) {
    var components = data.results[0].address_components;
  8. Vinay Aggarwal renamed this gist Feb 21, 2016. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions latlng.js → latlng_to_city.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    /*
    * Pass it your string of latlong and it'll return a promise to come back with the locality, city.
    *
    * getCity('12.9715987,77.5945627').done(function(cityName){
    * $elem.val( cityName )
    * });
    *
    * Info: jQuery is required for $.Deferred() to work!
    */

    function getCity(latlng) {
    var def = $.Deferred();

  9. Vinay Aggarwal created this gist Feb 21, 2016.
    26 changes: 26 additions & 0 deletions latlng.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    function getCity(latlng) {
    var def = $.Deferred();

    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?latlng=12.9715987,77.5945627').done(function(data){
    var location;
    if( data.results && data.results.length ) {
    var components = data.results[0].address_components;

    for(var i = 0; i < components.length; i++) {
    if( ~components[i].types.indexOf('locality') ) {
    location = components[i].long_name;
    break;
    }
    }

    if(location) return def.resolve(location);
    }

    return def.reject();
    })
    .fail(function(){
    def.reject();
    });

    return def.promise();
    }