1 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
JSON 是一種數(shù)據(jù)交換格式,而不是一種編程語言。當(dāng)您在字符串中包含諸如“new google.maps.LatLng...”之類的內(nèi)容時(shí),就是這樣:一個(gè)字符串。它沒有任何意義——即使有,您的 PHP 代碼也不會(huì)返回任何內(nèi)容,您的 JavaScript 代碼也不會(huì)執(zhí)行任何內(nèi)容。
不過,您走在正確的軌道上,所以讓我們做一些小改動(dòng)。
在你的 PHP 中,你可以這樣做:
<?php
function get_coordinates() {
$hotpointquery = query("SELECT `locationLatitude`, `locationLongitude` FROM `location_values` ");
confirm($hotpointquery);
while ($row = fetch_array($hotpointquery)) {
$coordinates = [$row['locationLatitude'], $row['locationLongitude']];
}
return json_encode($coordinates);
}
在頁面的后面,在<script>元素中,您可以讓 PHP 打印出該數(shù)組,然后 JS 可以使用map函數(shù)將其操作為您要查找的對(duì)象:
function getPoints() {
var coords = <?= get_coordinates() ?>
var points = coords.map(function(coord) {
return new google.maps.LatLng(coord[0], coord[1]);
});
return points;
}
- 1 回答
- 0 關(guān)注
- 140 瀏覽
添加回答
舉報(bào)