Skip to content

Instantly share code, notes, and snippets.

@dody87
Forked from danasilver/citystategeo.js
Last active February 13, 2020 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dody87/12897208a72147c2319c0bf7cbc200ad to your computer and use it in GitHub Desktop.
Save dody87/12897208a72147c2319c0bf7cbc200ad to your computer and use it in GitHub Desktop.
Get only city and state from Google Maps API Reverse Geocoder
_getCityState = function(resp){
var res = '';
if (resp.status == 'OK') {
if (resp.results[1]) {
var city=false,state=false;
for (var i = 0; i < resp.results.length; i++) {
if ((!city || !state) && resp.results[i].types[0] === "locality") {
city = resp.results[i].address_components[0].short_name,
state = resp.results[i].address_components[2].short_name;
res = city + ", " + state;
}
}
}
}
};
@rickyriccs
Copy link

var x, lat, lng, city, state, country, geocoder, latlng;

$(document).ready(function() {
    getLocation();
});

x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    lat = position.coords.latitude;
    lng = position.coords.longitude;
    // inprange = parseInt($('#range_3').prop("value") * 1000);
    geocodeLatLng(lat, lng);
}

function showError(error) {
    switch (error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation.";
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable.";
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out.";
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred.";
            break;
    }
}

function geocodeLatLng(lat, lng) {
    geocoder = new google.maps.Geocoder();
    latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({
        'latLng': latlng
    }, function(results, status) {
        if (status === 'OK') {
            if (results[1]) {
                console.log(results);
                for (var i = 0; i < results[0].address_components.length; i++) {
                    for (var b = 0; b < results[0].address_components[i].types.length; b++) {
                        switch (results[0].address_components[i].types[b]) {
                            case 'locality':
                                city = results[0].address_components[i].long_name;
                                break;
                            case 'administrative_area_level_1':
                                state = results[0].address_components[i].long_name;
                                break;
                            case 'country':
                                country = results[0].address_components[i].long_name;
                                break;
                        }
                    }
                }
                alert(city + ' ' + state + ' ' + country)
            } else {
                alert("No results found");
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment