gpt4 book ai didi

jquery - 使用 MySQL、JSON 和 jQuery 插件 Gmap3 填充谷歌地图标记

转载 作者:行者123 更新时间:2023-11-29 02:26:43 25 4
gpt4 key购买 nike

我在显示从 MySQL 数据库中提取数据的标记时遇到问题。尽管 JSON 有效,但 JSON 格式不正确。我想至少有两种方法可以解决我的问题,1. 更改 MySQL 查询或 2. 在使用 Gmap3 构建 Google Map 之前使用 jQuery 操作 JSON 输出。我与他们中的任何一个都斗争..

my-json.php 中的 MySQL 查询和 JSON 编码:

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();
}

所以这似乎工作正常,我得到了一个像这样的有效 JSON:

{
"center":[
51.9502649,
11.6922735
],
"tatorte":[
{
"values":{
"title":"my title",
"lat":"51.7920562",
"lng":"11.141447999999968",
"subline":"my sublime",
"teaser":"my teaser text"
}
}
]
}

显然,格式应该是这样的:

{
"center": [
46.578498,
2.457275
],
"tatorte": [
{
"lat": 49.00408,
"lng": 2.56228,
"data": {
"title":"my title",
"subline":"my sublime",
"teaser":"my teaser text"
}
}
]
}

所以我尝试在 jQuery 中加载和操作我的 JSON:

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);
}
});
}

然后,使用 Gmap3 插件显示它们:

$(function(){
loadData();
});

function display(center, tatorte) {
$('#map_canvas').gmap3({
map:{
options:{
center: center,
zoom:7,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
},
marker: {
values: tatorte
}
});
}

中心点显示正确,与 map 本身相同。只有标记丢失了。而且我错过了一些正确的事情......我想这很容易,但我对 jQuery/javascript 或如何更改 MySQL 查询以获得标记值的正确格式没有太多经验.任何人?非常感谢帮助。谢谢。

最佳答案

尝试调整您的 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);
}

关于jquery - 使用 MySQL、JSON 和 jQuery 插件 Gmap3 填充谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com