2014-01-07 79 views
0

我一直在使用下面的代碼一段時間了,並注意到它已停止工作,並引發錯誤。警報聲明爲空。地圖API是否已更改?我已經裝了 https://maps.googleapis.com/maps/api/js?sensor=falsehttps://maps.gstatic.com/intl/en_us/mapfiles/api-3/15/5/main.js谷歌地圖API返回空郵政編碼(用於工作)

function geo(){ 
    if(navigator.geolocation) { 
     var fallback = setTimeout(function() { fail('10 seconds expired'); }, 10000); 
     navigator.geolocation.getCurrentPosition(
      function (pos) { 
       clearTimeout(fallback); 
       console.log('pos', pos); 
       var point = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
       new google.maps.Geocoder().geocode({'latLng': point}, function (res, status) { 
        if(status == google.maps.GeocoderStatus.OK && typeof res[0] !== 'undefined') { 
         var zip = res[0].formatted_address.match(/,\s\w{2}\s(\d{5})/); 
alert(zip); 
         var homecity; 
         var homezip; 

         if((zip[1]>21201)&&(zip[1]<21298)) { 
           //document.getElementById('geo').innerHTML = "Baltimore "+zip[1]; 
           homecity = "Baltimore"; 
           homezip = zip[1]; 
           //$("._res").html(homecity+" "+homezip); 
           window.location.href = "?city="+homecity+"&zip="+homezip; 
         } 
         if((zip[1]>20001)&&(zip[1]<20886)) { 
           //document.getElementById('geo').innerHTML = "Baltimore "+zip[1]; 
           homecity = "D.C."; 
           homezip = zip[1]; 
           //$("._res").html(homecity+" "+homezip); 
           window.location.href = "?city="+homecity+"&zip="+homezip; 
         } 
         if((zip[1]>19019)&&(zip[1]<19255)) { 
           //document.getElementById('geo').innerHTML = "Baltimore "+zip[1]; 
           homecity = "Philadephia"; 
           homezip = zip[1]; 
           //$("._res").html(homecity+" "+homezip); 
           window.location.href = "?city="+homecity+"&zip="+homezip; 
         } 


        } 
       }); 
      }, function(err) { 
       fail(err.message+" WTF"); 
      } 
     ); 
    } 
+0

哪種瀏覽器,你的工作嗎?請參閱:[this](http://stackoverflow.com/questions/18980037/navigator-geolocation-getcurrentposition-not-working)和[that](http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition - 有時 - 作品 - 有時 - 不) – Raptor

+0

我在最新的公開Chrome –

回答

0

簡單的修復,只是改變在res 0到1:

  var zip = res[1].formatted_address.match(/,\s\w{2}\s(\d{5})/); 
相關問題