I am new to this but I have tried to put some code together, however I’m getting errors flagging up and it doesn’t seem to work. I would like some input to help debug it.
I have a cookie called “country” saved with a value of “AZ”. I want to pull this cookie using javascript and store it as a variable, then use it in my google map page. This is the code I have:
// Setup variable with blank value
var country_code ='';
// Use read_cookie function to pull "country" value from cookie
if (country_code) var country_code = read_cookie('country');
// Make sure its value is lower case (required)
country_code = country_code.toLowerCase();
And then a bit further down:
var input = document.getElementById('loc');
var options = { types: [], ComponentRestrictions({ 'country': country_code });};
var autocomplete = new google.maps.places.Autocomplete(input, options);
The first error is that I have too many brackets on the “componentrestriction” line. How should this be done? It looks like I am closing them correctly, I encase the componentrestrictions with ({}); and var options with {};
EDIT: This is the read cookie function code I found on stackoverflow:
// Reading Cookie V0.1
function read_cookie(key){
var result;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
}
You have a semicolon inside the statement and to much brackets after ComponentRestrictions.
Write it this way: