【发布时间】:2015-06-12 10:19:16
【问题描述】:
我想知道是否有办法将数组及其内容传递到另一个页面以供使用。
我正在使用一个数组来存储用于在谷歌地图上绘制折线的坐标。该数组在一页上工作正常,但是当我尝试调用该数组以在另一张地图上绘制折线点时,似乎该数组已被清空并且没有绘制折线点。
我尝试将 localStorage 与 JSON stringify 一起使用,但遇到了许多问题,谷歌地图不接受 ether 值,因为它们包含不期望的值,或者因为它根本无法识别格式。
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
【问题讨论】:
-
您可以使用会话存储。 geekchamp.com/html5-tutorials/18-html5-session-storage 或 davidwalsh.name/html5-storage 那里有很多资源。
-
I have attempted to use localStorage with JSON stringify but came across many many issues where google maps wont accept the values ether because they contain values its not expecting, or because it simply cant recognise the format... 传回数据时是否取消了字符串化? stackoverflow.com/questions/11171746/reverse-of-json-stringify -
你有没有得到这个工作?
标签: javascript arrays google-maps cordova