所以我的代码出了问题,我尝试从我从数据库下载的变量设置谷歌街景位置。它的数据库和php方面有效,但当我尝试将其分配给“panorama.setPosition(prizeLocation);”时它给我错误“InvalidValueError:setPosition:not LatLng或LatLngLiteral:not a Object”。我试过并环顾四周但是每个人都遇到了setCenter的问题而没有使用setPosition。任何帮助表示赞赏
由于
HTML代码:
<script>
var prizeLocation;
function downloadCoords() {
$.ajax({
type: 'GET',
url: 'prizeCoordsTransformer.php',
dataType: "json",
success: function(data) {
console.log(data);
prizeLocation = data.prizeLocation;
console.log(prizeLocation);
},
error: function(data) {
console.log(data);
}
});
}
</script
(稍后在剧本中)
<script>
setTimeout(function() {
console.log(prizeLocation);
panorama = map.getStreetView();
panorama.setPosition(prizeLocation);
panorama.setPov({
heading: 265,
pitch: 0
});
panorama.setVisible(true);
panorama.setOptions({
fullscreen: true,
enableCloseButton: false
});
}, 2000);
</script>
PHP代码:
<?php
ini_set('display_errors', 0);
error_reporting(E_ERROR);
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "db";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
mysqli_select_db($conn, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";
$query = "SELECT prizeLocation FROM prizeLocation";
$result = mysqli_query($conn, $query);
$result1 = mysqli_fetch_row($result);
echo json_encode(array('prizeLocation'=>$result1[0]));
mysqli_close($conn);
?>
P.S。这里的一切都很完美,但是prizeLocation变量。所以,如果我要将变量更改为“var prizeLocation = {lat:-43.533476,lng:172.636581};”或者它会起作用的东西
答案 0 :(得分:1)
PHP是否设置了mime类型application/json
?
是否有console.log(prizeLocation);在成功中打印一个字符串或一个对象 - 如果仍然是一个字符串,请执行prizeLocation = JSON.parse(data.prizeLocation);
{"lat": -43.533476, "lng": 172.636581}
喜欢这个
var pl = {"lat": -43.533476, "lng": 172.636581};
console.log(pl);
&#13;
试试这个
<?PHP
header("content-type:application/Json");
$arr = array('lat' => -43.533476, 'lng' => 172.636581);
echo json_encode($arr);
?>