-1

I'm trying to figure out if the user is near a city ( within a 50 mile radius ) using geolocation.

Here is what I have. It is a script that checks if I am in any of the cities in the array. I am in a city in the New York City array of cities, but it is evaluating to the else as I see the function taking place.

Here is the script:

if('geolocation' in navigator){
var lat, lon;
var SFlocations = ['richmond', 'berkely', 'daly city', 'oakland', 'san francisco'];
var NYlocations = ['new york', 'new york city', 'new jersey', 'newark', 'kendall park', 'fraklin park', 'new brunswick', 'east brunswick', 'edison', 'manhattan', 'queen', 'staten island', 'bronx'];
navigator.geolocation.getCurrentPosition(function(pos){
    lat = pos.coords.latitude;
    lon = pos.coords.longitude;
    $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lon+'&sensor=true').done(function(res){
        var results = res.results;
    var address = results[2].address_components;
    var city = address[1].long_name.toLowerCase();
    if(jQuery.inArray(city, SFlocations) !== -1) $('#input').css('backgroundImage', 'url(/cityBackground/SanFrancisco/day.png)').css('backgroundPosition', 'bottom left').css('backgroundSize', 'cover');
    else if(jQuery.inArray(city, NYlocations) !== -1) $('#input').css('backgroundImage', 'url(/cityBackgrounds/NYC/night.png)');
    else setBGPerTime();
    });

});
}
else alert("don't supports geolocation");

Here is a JS Fiddle: http://jsfiddle.net/s9VrT/

It seems logical and I can't figure out why it is evaluating to false.

I would greatly appreciate any and all help!

EDIT: So it seems I am grabbing the wrong part of the results array. It seems like the city is located here: results[0][address_components][2]. Is that true for anyone else that tests it from a different location? But when I try to grab that part of the array, it says address_components is not defined. Here is a fiddle: http://jsfiddle.net/s9VrT/4/

EDIT: Okay, I got the city using this var results = res.results; var city = results[0].address_components[2].long_name.toLowerCase();. However, I don't think this is the proper way. This gets the city for this location, but I don't think it will work for every location. How can I get the city regardless of what location the user is at?

gomangomango
  • 661
  • 1
  • 10
  • 29
  • So what is returned from Google exactly? `console.log(city)` – epascarello Mar 28 '14 at 19:36
  • @epascarello middlesex county. Which is the name of the county, but not the city. – gomangomango Mar 28 '14 at 19:44
  • Don't hard code those array indices. You need to look for the array element with the correct ["type"](https://developers.google.com/maps/documentation/geocoding/#Types) – geocodezip Mar 28 '14 at 20:16
  • @geocodezip okay, postal code sounds good. How do I get that from the results array then? – gomangomango Mar 28 '14 at 20:34
  • possible duplicate of [How to get city from coordinates?](http://stackoverflow.com/questions/17830686/how-to-get-city-from-coordinates) – geocodezip Mar 28 '14 at 21:13
  • @geocodezip Thank you very much! I believe that is exactly what I need. Or something I can build from. I'll test it tomorrow and comment here. – gomangomango Mar 29 '14 at 05:29

1 Answers1

-1

Here is what finally ended up working for me, checking the postal codes.

It is a combination of my code and the code here: How to get city from coordinates?

var NYlocations = ['10453', '10457', '10460', '10458', '10467', '10468', '10451', '10452', '10456', '10454', '10455', '10459', '10474', '10463', '10471', '10466', '10469', '10470', '10475', '10461', '10462', '10464', '10465', '10472', '10473', '11212', '11213', '11216', '11233', '11238', '11209', '11214', '11228', '11204', '11218', '11219', '11230', '11234', '11236', '11239', '11223', '11224', '11229', '11235', '11201', '11205', '11215', '11217', '11231', '11203', '11210', '11225', '11226', '11207', '11208', '11211', '11222', '11220', '11232', '11206', '11221', '11237', '10026', '10027', '10030', '10037', '10039', '10001', '10011', '10018', '10019', '10020', '10036', '10029', '10035', '10010', '10016', '10017', '10022', '10012', '10013', '10014', '10038', '10280', '10002', '1003', '10009', '10021', '10028', '10044', '10128', '10023', '10024', '10025', '10031', '10032', '10033', '10034', '10040', '11361', '11362', '11363', '11364', '11354', '11355', '11356', '11357', '11358', '11359', '11360', '11365', '11366', '11367', '11412', '11423', '11432', '11433', '11434', '11435', '11436', '11101', '11102', '11411', '11413', '11422', '11426', '11427', '11428', '11439', '11414', '11425', '11416', '11417', '11418', '11419', '11420', '11421', '11368', '11369', '11370', '11372', '11373', '11377', '11378', '10302', '10303', '10310', '10306', '10307', '10308', '10309', '10312', '10301', '10304', '10305', '10314', '07101', '07102', '07103', '07104', '07105', '07106', '07107', '07108', '07112', '07114', '07175', '07184', '07188', '07189', '07191', '07192', '07192', '07193', '07193', '07195', '07198', '07199', '08824', '08823', '08816', '08901', '08902', '08903', '08904', '08905', '08906', '08933', '00989', '08817', '08818', '08820', '08837', '08899', '08906'];
                    navigator.geolocation.getCurrentPosition(function(pos){
                        lat = pos.coords.latitude;
                        lon = pos.coords.longitude;
                        $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lon+'&sensor=true').done(function(data){
                            $.each( data['results'],function(i, val){
                                $.each( val['address_components'],function(i, val){
                                    if(val['types'] == "postal_code"){
                                        if(val['long_name']!="") city = val['long_name'].toLowerCase();
                                        else city = 'unknown';
                                    }
                                });
                            });
                            if(jQuery.inArray(city, SFlocations) !== -1) $('#input').css({'backgroundImage':'url(cityBackground/SanFrancisco/day.png)', 'backgroundPosition':'bottom left', 'backgroundSize':'cover'});
                            else if(jQuery.inArray(city, NYlocations) !== -1) $('#input').css({'backgroundImage':'url(cityBackgrounds/NYC/night.png)', 'backgroundPosition':'bottom left', 'backgroundSize':'cover'});
                            else setBGPerTime();

                        });

                    });
Community
  • 1
  • 1
gomangomango
  • 661
  • 1
  • 10
  • 29