So i have a page called region.php, which in the header, loads JQuery, the google maps API, and the custom Jquery functions I use for my app in a file called functions.js.
In region.php, I pull a few rows from the MySQL DB and store them in a php array, $regions.
In my functions.js file, I'm trying to json_encode the $regions array, and pass it to my function, to plays all of the lats and longitudes of the $regions array on the google map. However, IM having trouble getting the Php array into Javascript.
I had been following this but it doesnt seem to be working for me: Iterating through a PHP array in jQuery?. Doesn't seem like the javascript can work with the php in the example they give
any ideas? (and I suppose if im going about this all wrong -- what is the best way to get the php array into javascript?
Region.php $regions = get_regions();
foreach($regions as $region) :
print $region['name'];
endforeach;
print "<div id='map_view_canvas' style=\"width:300px; height:300px; \"></div>";
functions.js
$(document).ready(function() {
initialize_view_map();
}
function initialize_view_map()
{
var latlng = new google.maps.LatLng(9.3939, 20.57268);
var myOptions = {
zoom: 2,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_view_canvas"), myOptions);
var mapPoints = <?php echo json_encode($regions) ?>;
$.each(mapPoints, function (i, elem) {
var newLatLng = new google.maps.LatLng(elem.latitude, elem.longitude);
var marker = new google.maps.Marker({
position: newLatLng,
map: map,
draggable:false,
animation: google.maps.Animation.DROP
});
});
}