I've asked this before but simplifying the question. I have a JSON with geo-coordinates that I'd like to map in d3/leaflet:
Here's the structure, when console.logging the JSON allSFEvents
:
(36) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
And digging into one item:
@context: "http://schema.org"
@type: "MusicEvent"
endDate: "2019-01-27"
image: ""
location:
@type: "Place"
address: "San Francisco, CA"
geo:
@type: "GeoCoordinates"
latitude: 37.775
longitude: -122.4183333
__proto__: Object
name: "Natoma Cabana"
__proto__: Object
name: "Winter Olympics"
performer: {url: "https://www.bandsintown.com/a/217960-
winter-olympics?came_from=244", image: "", @type:
"MusicGroup", name: "Winter Olympics"}
startDate: "2019-01-27"
url: "https://www.bandsintown.com/e/1013280443-winter-olympics-at-natoma-cabana?came_from=244"
__proto__: Object
When I try to convert to latLong coordinates:
allSFEvents.forEach(function(d) {
d.latLong = new L.LatLng(allSFEvents.location.geo[0],//first position is latitude
allSFEvents.location.geo[1];//second position is longitude
console.log(d.latLong)
})
It gives me an error saying:
Cannot read property 'geo' of undefined
How do I loop through each item and run the L/.LatLong here? I seem to be stuck. The goal is to get distinct lat/long pairs for each item, in order to map it. What can I try next?
the forEach-function goes thru every element of your list and the parameter of the function is the one item of the list. So d is one item of your allSFEvents-Array.
https://www.w3schools.com/jsref/jsref_forEach.asp
So try the following:
allSFEvents.forEach(function(d) {
//here replace allSFEEvents with d then location should be set. And replace geo[0] with geo.latitude and geo[1] with geo.longitude because geo is not an array
d.latLong = new L.LatLng(d.location.geo.latitude,//first position is latitude
d.location.geo.longitude;//second position is longitude
console.log(d.latLong)
})