我想知道是否有一种方法可以将数组及其内容传递到另一个页面以供使用。
我正在使用一个数组来存储用于在谷歌地图上绘制折线的坐标。该数组在一页上工作正常,但是当我尝试调用该数组在另一张地图上绘制折线点时,似乎该数组已被清空并且没有绘制折线点。
我尝试将 localStorage 与 JSON stringify 一起使用,但遇到了许多问题,谷歌地图不会接受以太值,因为它们包含不期望的值,或者因为它根本无法识别格式。
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);
}
此代码从给定坐标构建数组,该函数从 watchPosition 函数的 onSuccess 中调用
storeLatLng(lat, lon);
然后我想在以下函数中使用数组值
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);
}
}
这被称为另一个页面的onLoad