I have problems to display markers which data I pull from a MySQL database. The JSON format is not right, although the JSON is valid. I guess there are at least 2 approaches to my problem, 1. to alter the MySQL query or 2. to manipulate the JSON output with jQuery before constructing the Google Map with Gmap3. I struggle with either of them..
MySQL query and JSON encode in my-json.php:
try {
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$tatorte = $db->query("
SELECT p.post_title as title,
max(case when pm.meta_key = '_cmb_tatort_map_latitude' then pm.meta_value end) as lat,
max(case when pm.meta_key = '_cmb_tatort_map_longitude' then pm.meta_value end) as lng,
max(case when pm.meta_key = '_cmb_subline' then pm.meta_value end) as subline,
max(case when pm.meta_key = '_cmb_teaser_text' then pm.meta_value end) as teaser
FROM mob_posts p join
mob_postmeta pm
on pm.post_id = p.ID
WHERE pm.meta_key in ('_cmb_tatort_map_latitude', '_cmb_tatort_map_longitude', '_cmb_subline', '_cmb_teaser_text') and
p.post_type = 'tatort'
group by p.id
ORDER BY p.id DESC
LIMIT 100;
");
$rows = array();
while ($locations = $tatorte->fetch_assoc()) {
$rows[] = array('values' => $locations);
}
$json = array(
"center" => array(51.95026490,11.69227350), // center map at a given location
"tatorte" => $rows
);
header('Content-Type: application/json; charset=UTF-8;');
echo json_encode( $json );
} catch (Exception $e) {
echo $e->getMessage();
}
So this seems to be working fine, I get a valid JSON like so:
{
"center":[
51.9502649,
11.6922735
],
"tatorte":[
{
"values":{
"title":"my title",
"lat":"51.7920562",
"lng":"11.141447999999968",
"subline":"my sublime",
"teaser":"my teaser text"
}
}
]
}
Apparently, the format should be rather like this:
{
"center": [
46.578498,
2.457275
],
"tatorte": [
{
"lat": 49.00408,
"lng": 2.56228,
"data": {
"title":"my title",
"subline":"my sublime",
"teaser":"my teaser text"
}
}
]
}
So I try to load and manipulate my JSON in jQuery as so:
function loadData(){
var markers = [];
$.ajax({
url:'my-json-load.php',
success:function(data){
var markers = [];
$.each(data, function(key, val){
var position = [val.lat, val.lng];
markers.push({ latLng: position });
});
display(data.center, data.tatorte);
}
});
}
Then, to use the Gmap3 plugin to display them:
$(function(){
loadData();
});
function display(center, tatorte) {
$('#map_canvas').gmap3({
map:{
options:{
center: center,
zoom:7,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
},
marker: {
values: tatorte
}
});
}
The center point is displayed correctly, same as the map as such. Only the markers are missing. And I am missing something to do it right... I guess it is quite easy, but I don't have much experience with jQuery/javascript or how to alter the MySQL query in order to get to the right format for the marker values. Anyone? Help greatly appreciated. Thanks.
Try tweaking your php.
while ($locations = $tatorte->fetch_assoc()) {
$lat = $locations['lat'];
$lng = $locations['lng'];
$values = json_encode( array('title' => $locations['title'], 'subline' => $locations['subline'], 'teaser' => $locations['teaser']) );
$rows[] = array('lat' => $lat, 'lng' => $lng, 'values' => $values);
}