Reputation: 103
I'm trying to populate my Google Map API with the Results from my database. My plan is to pass the query result to the Javascript so that it can plot the Marker on the Lat and Lng output.
///Model
public function getLatLng(){
$this->db->select('*');
$this->db->from('tbl_loc');
$query = $this->db->get();
return json_encode($query->result());
}
///Controller
public function populateMaps(){
$data['loc'] = $this->user_model->getLatLng();
$this->load->view('maps', $data);
}
///View
<div id="getAddress">
<?php foreach($loc as $lc){
echo $lc;
?>
</div>
///JS
var getAllData = JSON.parse(document.getElementById('getAddress').innerHTML);
showAllLoc(getAllData);
function showAllLoc(getAllData){
Array.prototype.forEach.call(getAllData, function()){
var marker = new google.maps.Marker({
position : new google.maps.LatLng(getAllData.Lat, getAllData.Lng),
map : map
});
}
Am i missing something? Or is there any problem with my code? I'm kind of new to Codeigniter, so any help will do. Thank you! :D
EDIT: I have done the json_encode when returning the possible output in the model, just like the one in here. What i'm trying to figure out is why is it not displaying the results in the div.
Upvotes: 1
Views: 529
Reputation: 1146
You can use this code it works I tested it.
$tbl_loc = $this->db->get('tbl_loc')->result_array();
return json_encode($tbl_loc);
Upvotes: 2