dealing with geocoder errors

29 Views Asked by At

Newbie here with javascript. I am following an online tutorial on googlemaps api. The code below works well but fails to get lat and lng for malformed addresses. I have read that this is a problem with how json parses address with special characters. However, I would like to redirect the user to a separate error page if the geocoder throws an error. I have tried below to use location.replace("../error.php") but I do not get the redirect. I could use some help

function codeAddress(cdata) {
     Array.prototype.forEach.call(cdata, function(data){
            var address = data.address;
            geocoder.geocode( { 'address': address}, function(results, status) {
                if (status == 'OK') {
                    map.setCenter(results[0].geometry.location);
                    var points = {};
                    points.id = data.id;
                    points.lat = map.getCenter().lat();
                    points.lng = map.getCenter().lng();
                    updateLocationsWithLatLng(points);
                }
             else {
                    location.replace("../error.php");
                 alert('Geocode was not successful for the following reason: ' + status);
                }
            });
    });
    }
0

There are 0 best solutions below