How do I properly format this JavaScript Array/Object for Google Maps? -
having trouble google maps api in terms of structuring data coming off of ajax request. here's i'm @ 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 = items.length; ( i; i--; ) { item = items[ ]; 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 understand, google maps wants data in json , not in array. can tweak get there?
new revised, working code:
core.api.collection( hdata.url, format, datatype ).done(( response ) => { const items = response.items; if ( response.items ) { let = items.length; ( i; i--; ) { item = items[ ]; 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); } } });
you trying treat single marker array of markers.
const googlemapmarkers = new google.maps.marker( markerdata );
this won't work.
why not create instance of google.maps.marker
iterating through items , save push them array. can set map of each in iteration.
var googlemapmarkers = []; ( i; i--; ) { item = items[ ]; markerdata.push({ position: new google.maps.latlng(item.location.maplat, item.location.maplng), title: item.location.addresstitle }); markerdata.setmap(googlemap); googlemapmarkers.push(markerdata); }
this assuming markerdata valid google map marker. if isn't may have little more code construct object, it's done , plenty examples in api reference.
Comments
Post a Comment