I researched this subject many times but I could not find the right answer to my question. Let me explain it.
I\'m creating an app with the Google Maps API where I want
Try:
PHP:
function getMarkers(){
$response = array();
foreach ($query as $post) {
$response[] = array(
'name' => $post['naam'],
'lat' => $post['plaats'],
'lng' => $post['categorie'],
);
}
echo json_encode($response);
}
then JS:
function addMarkers(json){
var title =json[index]["naam"];
var lat =json[index]["plaats"];
var lng =json[index]["categorie"];
marker = new google.maps.Marker({
position: new google.maps.LatLng (lat, lng),
map: YOUR_MAP
});
}
jQuery.getJSON("URL OF PAGE",{
format: "json",
dataType: 'json',
contentType: "application/json; charset=utf-8"},
function(json){addMarkers(json);
});
better, to send the info with json and retrieve it with jQuery's getJSON
, this may need some tweaking.. But I have created what your talking about. Shout if any further help is required.
You May have to look into this a tiny bit to configure it..
GOOD LUCK!
$post
is your iteration variable, so it only contains one element of the array at a time. If you want the entire array, assign that:
var bedrijven = <?php echo json_encode($query); ?>;
The examples on http://us1.php.net/json_encode should be helpful. It looks like your $post should be an associative array:
$post[] = ("name"=>"test","lat"=>52.351753, "lon"=> 5.002035); ?>
var bedrijven = <?php echo json_encode($post); ?>;
outputs:
var bedrijven = [{"name":"test","lat":52.351753,"lon":5.002035}];