C
C
Cyber_bober2015-03-22 08:49:30
JavaScript
Cyber_bober, 2015-03-22 08:49:30

How to save coordinates to an array in a separate file?

Hello, in the application I enter an address and in return I get the coordinates and the name of the autonomous region.
I get 3 lines in the output

results[0].formatted_address.value
results[0].address_components[2].long_name.value
results[0].geometry.location.value

I need to save them to an array in the main.js file, the
array looks like
var Points = [
    {
        object: "ул.Стромынка, д.7",
        district:   "Центральный автономный округ",
        point: [56.0091086,92.8714365]
    },

]

How to write this data to an array so that it persists even after the page is reloaded?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2015-03-22
@Cyber_bober

var Points = [ /* коллекция точек */ ];

function save(points) {
  localStorage.setItem('points', JSON.stringify(points));
}

function load() {
  var points = localStorage.getItem('points');
  
  return points ? JSON.parse(points) : [];
}

D
Dmitry, 2015-03-22
@thewind

The data needed after the page is reloaded must be stored somewhere.
And JS is a client-side language, so you can save data using it in several ways some identifier?)
2. In a cookie, but then the data will be available only on the current computer, in the current browser (+ as much time as you specify)
3. In localStorage - the storage method is identical to cookies, the time is unlimited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question