2014-03-28 41 views
-1

我想弄清楚用户是否在使用地理定位的城市附近(在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/

这似乎是合乎逻辑,我想不通为什么它被评估为false。

我将不胜感激任何和所有的帮助!

编辑:所以看来我抓住了结果数组的错误部分。看起来城市位于这里:results [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();的城市。但是,我不认为这是正确的方式。这个位置获得了城市,但我认为它不适用于每个位置。 无论用户在哪个位置,我怎样才能获得城市?

+0

那么,什么是谷歌从返回到底是什么? 'console.log(city)' – epascarello

+0

@epascarello middlesex county。这是县的名字,但不是城市。 – gomangomango

+0

不要硬编码这些数组索引。您需要使用正确的[“type”](https://developers.google.com/maps/documentation/geocoding/#Types) – geocodezip

回答

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

         }); 

        }); 
相关问题