-1

So I am using geocode service from Google Map APIv3. I have restricted search around 'Switzerland/CH'.

But when I typed invalid characters like !x! or xz etc, I didn't get 'Invalid Address' rather I got 'Switzerland' as a result in address_components array but it returns 'Invalid Address' if country restriction was not applied.

But I want country restriction and also return an error result, If user types something invalid.See My Code is below.

My Code:

    function placeSearchBox() {
        var searchBoxText = document.getElementById('locationSearch').value;
        console.log('placeboxText',searchBoxText);
        if (searchBoxText.length > 0) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({
                address: searchBoxText,
                componentRestrictions: {
                    country: 'CH'
                }
            }, function (data, status) {
                console.log(data);
                if (status === google.maps.GeocoderStatus.OK && data.length > 0) {
                    searchResultOfGmapObj = data[0];
                    dispalySearchResult(data[0]);
                    console.log('GeoCodedAddress',data[0]);                                                        
                    vm.site.SiteAddress = searchBoxText;
                } else alert("Invalid address");
            });
        }
    }

So why is it not returning invalid address or Wrong Status?

1 Answers1

0

As a work-around for this problem I did following, but if anybody can tell reason of this problem and different solution please share.

function placeSearchBox() {
        var searchBoxText = document.getElementById('location-search').value;
        console.log('placeboxText',searchBoxText);
        if (searchBoxText.length > 0) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({
                address: searchBoxText,
                componentRestrictions: {
                    country: 'CH'
                }
            }, function (data, status) {
                console.log(data,status,searchBoxText);
                if (status === google.maps.GeocoderStatus.OK && data.length > 0) {
                    searchResultOfGmapObj = data[0];
                    var searchText = searchBoxText.toLowerCase();
                    if(searchText != 'switzerland' && searchResultOfGmapObj.address_components.length == 1 && searchResultOfGmapObj.address_components[0].long_name == 'Switzerland'){
                        alert("Invalid address");
                    }else{    
                        dispalySearchResult(data[0]);
                        console.log('GeoCodedAddress',data[0]);                                                        
                        vm.site.SiteAddress = searchBoxText;
                    }
                }
            });
        }
    }

So just the following codeblock I have added,

var searchText = searchBoxText.toLowerCase();
if(searchText != 'switzerland' && searchResultOfGmapObj.address_components.length == 1 && searchResultOfGmapObj.address_components[0].long_name == 'Switzerland'){
                    alert("Invalid address");
}