Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Maps - Getting points in Php array, to points on a map
    text
    copied!<p>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.</p> <p>In region.php, I pull a few rows from the MySQL DB and store them in a php array, $regions.</p> <p>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. </p> <p>I had been following this but it doesnt seem to be working for me: <a href="https://stackoverflow.com/questions/1205892/iterating-through-a-php-array-in-jquery">Iterating through a PHP array in jQuery?</a>. Doesn't seem like the javascript can work with the php in the example they give</p> <p>any ideas? (and I suppose if im going about this all wrong -- what is the best way to get the php array into javascript?</p> <p>Region.php $regions = get_regions(); </p> <pre><code>foreach($regions as $region) : print $region['name']; endforeach; print "&lt;div id='map_view_canvas' style=\"width:300px; height:300px; \"&gt;&lt;/div&gt;"; </code></pre> <p>functions.js</p> <pre><code>$(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 = &lt;?php echo json_encode($regions) ?&gt;; $.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 }); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload