我正在创建一个 Chrome 扩展程序,当您单击它时它会收集您的地理位置 - 同步数据,以便它存储您所在的过去 20 个经纬度坐标。但是 chrome.storage.sync.set 似乎不起作用!
var locations = [];
//get past locations and put them into 'locations' array
chrome.storage.sync.get("locations", function(data){
console.log("Getting Locations");
var index;
for (index = 0; index < data.length; ++index) {
locations.unshift(data);
}
});
//add current location to 'locations' array
navigator.geolocation.getCurrentPosition(function(position)
{
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
locations.unshift(initialLocation);
//sync locations to cloud
var obj = {};
obj["locations"] = locations;
chrome.storage.sync.set(obj, function() {
console.log("Location Saved");
console.log(locations);
});
因此,每次我运行此代码时 - 它应该 (a) 获取过去的位置 (b) 添加当前位置并 (c) 将其存储回云中。但是每次我运行程序时,'locations' 数组都保持大小 [1] 也就是没有得到 get/set !我查看了其他一些关于论点正确性的帖子,但我检查了我的,我相信它们是合理的。
发生了什么?!?任何人都可以阐明如何使此代码起作用吗?非常感谢我的代码英雄 :D