0

I've been using the following code for a while now and noticed it has stopped working and throwing an error. The alert says null. Has the maps API changed? I've loaded up https://maps.googleapis.com/maps/api/js?sensor=false and https://maps.gstatic.com/intl/en_us/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");
            }
        );
    }
pedrum golriz
  • 513
  • 8
  • 27
  • which browser are you working on? See: [this](http://stackoverflow.com/questions/18980037/navigator-geolocation-getcurrentposition-not-working) and [that](http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt) – Raptor Jan 07 '14 at 02:51
  • I'm on the latest public Chrome – pedrum golriz Jan 07 '14 at 02:54

1 Answers1

0

Simple fix, just change the 0 in the res to 1:

            var zip = res[1].formatted_address.match(/,\s\w{2}\s(\d{5})/);
pedrum golriz
  • 513
  • 8
  • 27