当前位置: 动力学知识库 > 问答 > 编程问答 >

Does Google Maps Javascript API v3 Geocoding Library overwrite localStorage keys with keys starting with "geocode:"?

问题描述:

I have Google Maps Javascript API v3 on a page and am using the Geocoding Library:

https://developers.google.com/maps/documentation/javascript/geocoding

The Code

In my google.maps.Geocoder.geocode callback function I am saving the lat,lng as JSON into the Chrome browser's window.localStorage like this:

if (localStorage.getItem('geocode:'+job.url) === null) {

geocoder.geocode({'address': job.address}, function(results, status) {

var json = JSON.stringify({

address: job.address,

lat: results[0].geometry.location.lat(),

lng: results[0].geometry.location.lng()

});

console.log('Set key:"geocode:'+job.url+'", value: '+json);

localStorage.setItem('geocode:'+job.url, json);

console.log('Get key:"geocode:'+job.url+'", value: '+localStorage.getItem('geocode:'+job.url));

});

}

The problem is my key is getting set to null outside the callback function.

After the above code runs and prints the two log messages, typing this in the Console shows the problem:

> localStorage.length;

1

> localStorage.key(0);

"geocode:123 street San Francisco CA USA"

> localStorage.getItem("geocode:123 street San Francisco CA USA");

null

Changing my localStorage key prefix to 'blah:' fixes the problem!

Does this mean the Google Maps Javascript API v3 Geocoding Library overwrites localStorage keys with a prefix "geocode:" after the callback function returns?

Extra background info:

I am including this script code on the page from a Chrome extension url like:

<script src="chrome:///blah" type="text/javascript"></script>

The call to google.maps.Geocoder.geocode and the callback function are called from a window.setTimeout handler.

分享给朋友:
您可能感兴趣的文章:
随机阅读: