地理定位脚本,用于确定我在城市时是否接近城市评估为假

时间:2014-03-28 19:32:49

标签: javascript jquery html5 google-maps geolocation

我正在尝试使用地理位置来确定用户是否在城市附近(半径50英里范围内)。

这就是我所拥有的。这是一个脚本,用于检查我是否在阵列中的任何城市。 我在纽约市的一个城市中,但是当我看到这个功能正在发生时,它正在评估其他城市。

这是脚本:

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");

这是一个JS小提琴:http://jsfiddle.net/s9VrT/

这似乎是合乎逻辑的,我无法弄清楚为什么它会被评估为假。

我非常感谢任何帮助!

编辑:所以我似乎抓住了结果数组的错误部分。看起来这个城市就在这里:结果[0] [address_components] [2]。对于从不同位置测试它的其他人来说,这是真的吗?但是当我试图抓住数组的那一部分时,它说没有定义address_components。这是一个小提琴:http://jsfiddle.net/s9VrT/4/

编辑:好的,我使用此var results = res.results; var city = results[0].address_components[2].long_name.toLowerCase();获得了城市。但是,我不认为这是正确的方法。这可以获得这个位置的城市,但我不认为它适用于每个位置。 无论用户所处的位置如何,我如何获得城市?

1 个答案:

答案 0 :(得分:-1)

这是最终为我工作的,检查邮政编码。

这是我的代码和代码的组合: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();

                        });

                    });