I need to make an array of values fetched from a Parse Server query and call a JS function that displays markers and infoWindows on a custom Google Map.
This is not a question about server side but mostly about how to create an array and pass it to JS function in PHP.
Here's where I perform my query:
try {
$query = new ParseQuery("Monsters");
$mArray = $query->find();
for ($i = 0; $i < count($mArray); $i++) {
// Get Parse Object
$mObj = $mArray[$i];
// Get name
$mName = $mObj->get('name');
// Get location
$mLocation = $mObj->get('location');
$mLat = $mLocation->getLatitude();
$mLng = $mLocation->getLongitude();
// Get points
$mPoints = $mObj->get('points');
// Here i need to make an array of $mName + $mPoints + $mLat + $mLng and call the addMonstersOnMap() JS function...
But my array should be like:
[name, points, 40.7143850, -72.0059756],
[name, points, 30.7143850, -44.0059756],
etc..
}
// error in query
} catch (ParseException $e){ echo $e->getMessage(); }
And here's my JS function (I'm missing how to pass the monsters array to it and call it):
function addMonstersOnMap(monsters) { // monsters IS THE ARRAY I NEED TO GET FROM MY PHP QUERY!
var centerCoords = new google.maps.LatLng(66.93828964, -53.64523124);
var mapOptions = {
zoom: 2,
scrollwheel: true,
center: centerCoords,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);
var marker, i;
var infowindow = new google.maps.InfoWindow();
// Add marker for each Monster
for (i = 0; i < monsters.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(monsters[i][1], monsters[i][2]),
map: map,
icon: monsters[i][3]
});
// click function to marker, pops up infowindow
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(monsters[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}// end FOR loop
google.maps.event.addDomListener(window, 'load', initialize);
}