简体   繁体   English

无法通过本地存储传递坐标

[英]Can't pass coordinates through localstorage

I'm trying to make a Google Maps application, where you can enter you location in an input and then get redirected to another page with a map that shows your location. 我正在尝试制作一个Google Maps应用程序,您可以在其中输入您的位置,然后将其重定向到带有显示您的位置的地图的另一个页面。 The problem is that when I'm saving the coordinates in a localstorage on one Javascript file its not able to read it out on the other one. 问题是,当我将坐标保存在一个Javascript文件的本地存储中时,它无法在另一个Javascript文件上读出。 Any ideas? 有任何想法吗?

Heres my Code 这是我的代码

Saving data: 保存数据:

 var location = document.getElementById('location-input').value;
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
    params:{
        address:location,
        key:'AIzaSyBsOGGokaWGD3m5y4uYYG_sv7U1T9nZ5HI'
    }
})
.then(function(response) {
    // Log full response
    console.log(response);

    var lat = response.data.results[0].geometry.location.lat;
    var lng = response.data.results[0].geometry.location.lng;

    sessionStorage.setItem('lat', lat);
    sessionStorage.setItem('lng', lng)

    console.log(lat)
})
.catch(function(error) {
    console.log(error);
})`

Retrieving data: 检索数据:

function initMap(){
var lat = sessionStorage.getItem('lat');
var lng = sessionStorage.getItem('lng')
console.log(lat)
})}

I want to add that passing a number directly or passing a word is no problem. 我想补充一点,直接传递数字或传递单词是没有问题的。

lat & lng are functions of a google.maps.LatLng . latlnggoogle.maps.LatLng函数。 Call them to get the value: 致电他们以获取价值:

var lat = response.data.results[0].geometry.location.lat();
var lng = response.data.results[0].geometry.location.lng();

live proof of concept: 现场概念证明:

better to encode your data before pssing them 更好地在对数据进行编码之前对它们进行编码

var coord = JSON.stringify({
  lat :  response.data.results[0].geometry.location.lat();
  lng  : response.data.results[0].geometry.location.lng();
});
sessionStorage.setItem('coord', coord);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM