我知道这个问题出现了很多,但是我已经做过三遍了,但由于某些原因,当我调用data.Latitude和data.Longitude时,由于将光标移到Latitude或Longitude上,标记仍然没有出现似乎未定义。我这样做是通过在youtube上找到的教程来指导自己,它虽然可以正常运行,但对我却不起作用。我是使用php的新手
这是我的php代码,用于从数据库中获取数据(即使用MySQL)。
<?php
class location
{
public $RouteID;
public $Latitude;
public $Longitude;
public $Battery;
public $DateObtained;
public $ID_DEVICE;
public $tableName = "geografia";
function setRouteID($RouteID) { $this->RouteID = $RouteID; }
function getRouteID() { return $this->RouteID; }
function setLatitude($Latitude) { $this->Latitude = $Latitude; }
function getLatitude() { return $this->Latitude; }
function setLongitude($Longitude) { $this->Longitude = $Longitude; }
function getLongitude() { return $this->Longitude; }
function setBattery($Battery) { $this->Battery = $Battery; }
function getBattery() { return $this->Battery; }
function setDate($DateObtained) { $this->DateObtained = $DateObtained; }
function getDate() { return $this->DateObtained; }
function setID_DEVICE($ID_DEVICE) { $this->ID_DEVICE = $ID_DEVICE; }
function getID_DEVICE() { return $this->ID_DEVICE; }
public function getLocations(){
require_once("db/DbConnect.php");
$sql = "SELECT RouteID,Latitude,Longitude,Battery,DateObtained FROM $this->tableName
WHERE RouteID IS NOT NULL";
while ($stmt = mysqli_query($connection, $sql)) {
return $stmt->fetch_all();
}
}
public function getAllLocations(){
$dbhost = 'localhost';
$dbName = 'intecespejo';
$dbuser = 'root';
$pass = '';
$connection = new mysqli($dbhost, $dbuser , $pass, $dbName);
require_once("db/DbConnect.php");
$sql = "SELECT Latitude,Longitude,Battery,DateObtained FROM $this->tableName";
while ($stmt = mysqli_query($connection, $sql)) {
return $stmt->fetch_all();
}
}
}
我在这里从locations.php(上面的代码)调用函数
第一个调用只是显示一个行数据,另一个显示所有内容,稍后我将对其进行更改。
<?php
require 'locations.php';
$Geo = new location;
$coll = $Geo->getLocations();
$coll = json_encode($coll, true);
echo '<div id="data">' . $coll . '</div>';
$allData = $Geo->getAllLocations();
$allData = json_encode($allData, true);
echo '<div id="allData">' . $allData . '</div>';
?>
这是我的JavaScript代码
var map;
function loadMap(){
var local = new google.maps.LatLng(18.487957, -69.961759) //{lat: 18.487957, lng: -69.961759}
map = new google.maps.Map(document.getElementById('map'), {
center: local,
zoom: 10
});
var marker = new google.maps.Marker({
position: local,
map: map
});
var cdata = JSON.parse(document.getElementById('data').innerHTML);
//console.log(cdata);
var DataLoc = JSON.parse(document.getElementById('allData').innerHTML);
showAllData(DataLoc);
}
function showAllData(DataLoc){
debugger;
Array.prototype.forEach.call(DataLoc, function(data){
//var localizacion = new google.maps.LatLng(data.Latitude, data.Longitude);
console.log(data);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.Latitude, data.Longitude),
map: map
});
})
}
对不起,如果我问的是与其他帖子相同的问题,但我只是想不出是什么问题,请提前感谢。