Is there a way to pass an array and its contents to another page for use?
I am utilizing an array to store coordinates for drawing a polyline on a Google map. The array works correctly on one page, but when attempting to call the array to draw the polyline points on a different map, it seems that the array is empty and no polyline points are drawn.
I tried using localStorage with JSON stringify, but faced several issues where Google Maps wouldn't accept the values because they contained unexpected values or were in an unrecognized format.
var googleLatLng = [],
latlngs = [];
function storeLatLng( lat, lng ) {
googleLatLng.push(new google.maps.LatLng(lat, lng));
latlngs.push([lat, lng]);
// setcoords = JSON.stringify(googleLatLng);
// localStorage.setItem('GoogleLatLng', setcoords);
// console.log(localStorage.getItem("GoogleLatLng"));
console.log(googleLatLng);
}
This code constructs the array from the provided coordinates, and the function is invoked within onSuccess of the watchPosition function as follows:
storeLatLng(lat, lon);
Subsequently, I aim to utilize the array values within the following function:
function finishedWalk() {
// storedCoords = localStorage.getItem('GoogleLatLng');
// if(storedCoords) storedCoords = JSON.parse(storedCoords);
navigator.geolocation.getCurrentPosition(onFinishedSuccess, onFinishedError);
}
function onFinishedSuccess(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
storeLatLng(latitude, longitude);
var mapOptions = {
zoom: 17,
center: coords,
mapTypeControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//create the map, and place it in the HTML map div
map = new google.maps.Map(document.getElementById("mapPlaceholder"), mapOptions);
if (googleLatLng.length > 0) {
var path = new google.maps.Polyline({
path: googleLatLng,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 5
});
path.setMap(map);
}
}
This function is called onLoad of another page.