簡體   English   中英

Google Maps API返回空郵政編碼(用於工作)

[英]Google maps API returning null zip code (used to work)

我一直在使用以下代碼一段時間,發現它已停止工作並引發錯誤。 警報顯示為空。 地圖API是否已更改? 我已經加載了https://maps.googleapis.com/maps/api/js?sensor=falsehttps://maps.gstatic.com/intl/zh_CN/mapfiles/api-3/15/5/main .js

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

簡單修復,只需將res中的0更改為1:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM