MHibbin
MHibbin

Reputation: 1185

Importing JSON for use as an array for GoogleMaps Markers

I'm still very new to Javascript, and I was looking for some advice.

I have set up the a google maps view to my liking, however it uses an array built into the javascript itself. basically this....

  var locations = [
     [9.449062, 7.950439, 0],
     [34.449062, 10.950439, 50]
  ];

Where the first two fields are location coordinates, and the third is a value I plan to use at another time.

I've looked at various tutorials, and they ever seem to not work with a local file (i.e. the fileis in the same directory on the filesystem), or the tutorial just does not seem to work.

I know there have been similar questions, however I can't seem to find what works for me.

I was wondering if someone could help me understand how to use a JSON file as an array like above.

Thanks in advance all.

MHibbin

:)

Upvotes: 0

Views: 150

Answers (1)

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

That looks like perfectly valid JavaScript assignment of object to variable. If you're using them from HTML page, just include script with your data first and then processing script later:

<script src="locations.js"></script>
<script src="worker.js"></script>

Where locations.js is exactly like quote you've provided and worker.js is something along the lines:

for(idx = 0; idx<locations.length; idx++){
  do_something_with(locations[idx])
}

Upvotes: 1

Related Questions