Having trouble with the Google Maps API in terms of structuring the data coming off of an AJAX request. Here's where I'm at currently:
const getMarkerData = function ( googlemap, $hook ) {
const hData = $hook.dataset;
const format = "json";
const dataType = "json";
const markerData = [];
let item = null;
core.api.collection( hData.url, format, dataType ).done(( response ) => {
const items = response.items;
if ( response.items ) {
let i = items.length;
for ( i; i--; ) {
item = items[ i ];
markerData.push({
position: new google.maps.LatLng(item.location.mapLat, item.location.mapLng),
title: item.location.addressTitle
});
}
}
const googleMapMarkers = new google.maps.Marker( markerData );
googleMapMarkers.setMap( googlemap );
});
};
From what I understand, Google Maps wants the data in JSON and not in an array. What I can tweak to get to get it there?
New revised, working code:
core.api.collection( hData.url, format, dataType ).done(( response ) => {
const items = response.items;
if ( response.items ) {
let i = items.length;
for ( i; i--; ) {
item = items[ i ];
marker = new google.maps.Marker({
position: new google.maps.LatLng(item.location.mapLat, item.location.mapLng),
title: item.location.addressTitle,
map: googleMap
});
googleMapMarkers.push(marker);
}
}
});