Javascript变量声明和谷歌地图
我对javascript非常陌生,但对代码过程有着合理的理解,我正在努力解决javascript中的全局变量问题 这个脚本位于一个网页中,该网页运行一个php页面来提取一些数据,然后对这些数据进行处理 数据有两种情况,第一种是在单元格中显示数据,这种方式非常有效,所以我知道变量正在设置,代码正在显示。第二点是我有问题。然后,我想获取其中两个变量GPSlat和GPSlon,并将它们传递给GoogleMaps脚本以更新/重新居中地图 使用下面的代码,如果我在谷歌地图代码中加入测试警报,我会将变量mycenter设置为0,0,如果我尝试只向GPSlon或GPSlat发出警报,我会得到未定义 我已经读了一堆关于变量的文章,但是我仍然缺少一些东西。有谁能把我推到正确的方向Javascript变量声明和谷歌地图,javascript,php,google-maps,global-variables,declaration,Javascript,Php,Google Maps,Global Variables,Declaration,我对javascript非常陌生,但对代码过程有着合理的理解,我正在努力解决javascript中的全局变量问题 这个脚本位于一个网页中,该网页运行一个php页面来提取一些数据,然后对这些数据进行处理 数据有两种情况,第一种是在单元格中显示数据,这种方式非常有效,所以我知道变量正在设置,代码正在显示。第二点是我有问题。然后,我想获取其中两个变量GPSlat和GPSlon,并将它们传递给GoogleMaps脚本以更新/重新居中地图 使用下面的代码,如果我在谷歌地图代码中加入测试警报,我会将变量my
<script type="text/javascript">
var GPSlat;
var GPSlon;
var temp;
var chill;
var HI;
var DP;
var pressure;
var heading;
var GPSalt;
var WindDir;
var WindSpd;
//check for browser support
if(typeof(EventSource)!=="undefined") {
//create an object, passing it the name and location of the server side script
var eSource = new EventSource("send_sse.php");
//detect message receipt
eSource.onmessage = function(event) {
//write the received data to the page
//document.getElementById("serverData").innerHTML = event.data;
var inputdata = event.data;
var array = inputdata.split("/");
if (array[0] != null){
temp = array[0];
}
if (array[1] != null){
chill = array[1];
}
if (array[2] != null){
HI = array[2];
}
if (array[3] != null){
DP = array[3];
}
if (array[4] != null){
hum = array[4];
}
if (array[5] != null){
heading = array[5];
}
if (array[6] != null){
if (array[6]=="*"){
GPSlat = 0;
} else {
GPSlat = array[6];
}
}
if (array[7] != null){
if (array[7]=="*"){
GPSlon = 0;
} else {
GPSlon = array[7];
}
}
if (array[8] != null){
if (array[8]=="*"){
GPSalt = 0;
} else {
GPSalt = array[8];
}
}
if (array[9] != null){
WindDir = array[9];
}
if (array[10] != null){
WindSpd = array[10];
}
if (array[11] != null){
pressure = array[11];
}
var br = "<BR>";
var output = "Temperature: " + temp + "C" + br + "Wind Chill: " + chill + "C" + br + "Dew Point: " + DP + "C" + br + "Heat Index: " + HI + "C" + br + "Humidity: " + hum + "%" + br + "Pressure: " + pressure + "hPa" + br + br + "Heading: " + heading + br + "Lattitude: " + GPSlat + br + "Longitude: " + GPSlon + br + "Altitude: " + GPSalt + br + br + "Wind Direction: " + WindDir + br + "Wind Speed: " + WindSpd + "kPh"
document.getElementById("serverData").innerHTML = output;
}
}
else {
document.getElementById("serverData").innerHTML="Whoops! No data from Sensor Computer received";
}
//GOOGLE MAPS CODE
var myCenter=new google.maps.LatLng([GPSlat],[GPSlon]);
function initialize()
{
var mapProp = {
center:myCenter,
zoom:10,
scaleControl:true,
streetViewControl:true,
overviewMapControl:true,
mapTypeId:google.maps.MapTypeId.HYBRID
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
var marker=new google.maps.Marker({
position:myCenter,
});
marker.setMap(map);
}
if (myCenter!="(0, 0)"){
google.maps.event.addDomListener(window, 'load', initialize);
} else {
document.getElementById("googleMap").innerHTML = "<center><BR><BR><BR><BR><BR><BR><BR><BR><BR>No GPS Data Available</center>"
}
</script>
根据下面的注释和建议,脚本得以运行,最终结果是将字符串变量作为浮动对象传递,并使用timeout/timeinterval延迟映射的加载,然后不断更新位置。现在工作正常
最终代码是:
<script type="text/javascript">
var GPSlat;
var GPSlon;
var temp;
var chill;
var HI;
var DP;
var pressure;
var heading;
var GPSalt;
var WindDir;
var WindSpd;
var inputdata;
var map;
var marker;
//check for browser support
if(typeof(EventSource)!=="undefined") {
var eSource = new EventSource("send_sse.php");
//detect message receipt
eSource.onmessage = function(event) {
//write the received data to the page
inputdata = String(event.data);
//var inputdata = "23.2/23.2/24.9/9.7/43/NW/-27.471011/153.023449/12.5/S/1.97/1028";
var array = inputdata.split("/");
if (array[0] != null){
temp = array[0];
}
if (array[1] != null){
chill = array[1];
}
if (array[2] != null){
HI = array[2];
}
if (array[3] != null){
DP = array[3];
}
if (array[4] != null){
hum = array[4];
}
if (array[5] != null){
heading = array[5];
}
if (array[6] != null){
if (array[6]=="*"){
GPSlat = 0;
} else {
GPSlat = array[6];
}
}
if (array[7] != null){
if (array[7]=="*"){
GPSlon = 0;
} else {
GPSlon = array[7];
}
}
if (array[8] != null){
if (array[8]=="*"){
GPSalt = 0;
} else {
GPSalt = array[8];
}
}
if (array[9] != null){
WindDir = array[9];
}
if (array[10] != null){
WindSpd = array[10];
}
if (array[11] != null){
pressure = array[11];
}
var br = "<BR>";
var output = "Temperature: " + temp + "C" + br + "Wind Chill: " + chill + "C" + br + "Dew Point: " + DP + "C" + br + "Heat Index: " + HI + "C" + br + "Humidity: " + hum + "%" + br + "Pressure: " + pressure + "hPa" + br + br + "Heading: " + heading + br + "Lattitude: " + GPSlat + br + "Longitude: " + GPSlon + br + "Altitude: " + GPSalt + br + br + "Wind Direction: " + WindDir + br + "Wind Speed: " + WindSpd + "kPh"
var dataset = "complete";
document.getElementById("serverData").innerHTML = output;
}
}
else {
document.getElementById("serverData").innerHTML = "Error: No Sensor Data Avaialable";
}
setTimeout(loadScript, 500);
setInterval(updatemarker,10000);
function initialize()
{
var mapProp = {
center: new google.maps.LatLng(parseFloat(GPSlat),parseFloat(GPSlon)),
zoom:10,
scaleControl:true,
streetViewControl:true,
overviewMapControl:true,
mapTypeId:google.maps.MapTypeId.HYBRID
};
map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);
positionmarker()
}
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' +
'callback=initialize';
document.body.appendChild(script);
}
function positionmarker(){
marker=new google.maps.Marker({
position:new google.maps.LatLng(parseFloat(GPSlat),parseFloat(GPSlon)),
title:"PiCam"
});
marker.setMap(map);
}
function updatemarker(){
var newpos = new google.maps.LatLng(parseFloat(GPSlat),parseFloat(GPSlon));
marker.setPosition(newpos);
map.panTo(newpos)
}
</script>
在这行中,var myCenter=new google.maps.LatLng[GPSlat],[GPSlon];
LatLng总是期望浮点值,所以请这样尝试
var myCenter=new google.maps.LatLng(parseFloat(GPSlat),parseFloat(GPSlon));
使用它并查看:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script type="text/javascript">
var GPSlat;
var GPSlon;
var temp;
var chill;
var HI;
var DP;
var pressure;
var heading;
var GPSalt;
var WindDir;
var WindSpd;
//check for browser support
if(typeof(EventSource)!=="undefined") {
var inputdata = "23.2/23.2/24.9/9.7/43/NW/-27.471011/153.023449/12.5/S/1.97/1028";
var array = inputdata.split("/");
if (array[0] != null){
temp = array[0];
}
if (array[1] != null){
chill = array[1];
}
if (array[2] != null){
HI = array[2];
}
if (array[3] != null){
DP = array[3];
}
if (array[4] != null){
hum = array[4];
}
if (array[5] != null){
heading = array[5];
}
if (array[6] != null){
if (array[6]=="*"){
GPSlat = 0;
} else {
GPSlat = array[6];
}
}
if (array[7] != null){
if (array[7]=="*"){
GPSlon = 0;
} else {
GPSlon = array[7];
}
}
if (array[8] != null){
if (array[8]=="*"){
GPSalt = 0;
} else {
GPSalt = array[8];
}
}
if (array[9] != null){
WindDir = array[9];
}
if (array[10] != null){
WindSpd = array[10];
}
if (array[11] != null){
pressure = array[11];
}
}
else {
}
alert(GPSlat);
alert(GPSlon);
var myCenter=new google.maps.LatLng(parseFloat(GPSlat),parseFloat(GPSlon));
function initialize()
{
var mapProp = {
center:myCenter,
zoom:10,
scaleControl:true,
streetViewControl:true,
overviewMapControl:true,
mapTypeId:google.maps.MapTypeId.HYBRID
};
var map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);
var marker=new google.maps.Marker({
position:myCenter,
});
marker.setMap(map);
}
if (myCenter!="(0, 0)"){
google.maps.event.addDomListener(window, 'load', initialize);
} else {
document.getElementById("map-canvas").innerHTML = "<center><BR><BR><BR><BR><BR><BR><BR><BR><BR>No GPS Data Available</center>"
}
</script>
<body>
<div id="map-canvas" style="float:left;width:100%;height:100%;">
</div>
</body>
好的,这当然有帮助,地图现在认为它得到了数字,所以它至少尝试渲染它,所以非常感谢你,因为我认为这将是我将面临的另一个问题。然而,在实际初始化映射之前使用警报的变量将两个值都显示为NaN,因此我假设它无法读取变量。我试图在声明变量后立即回显myCenter,它也是NaN,因此它无法访问变量,问题是为什么不能???进一步说,我想知道,是否因为Google Maps在页面上的所有内容之前初始化,所以在脚本运行以提取PHP值之前,这些值实际上是空的,但我希望一旦它真的读入了它们,或者我把这些隐藏字段弄得一团糟,它就会更新。将这两个值赋给这些变量,并在创建myCenter时使用它们,它应该可以工作。但在此之前,请提醒您分配给变量的值,以检查它们是否真的存在。隐藏字段是什么意思?很抱歉,对于Java来说非常陌生,如果我在声明myCenter变量之前直接发出警告,那么我就没有定义。当前的示例是:alertGPSlat alertGPSlon var myCenter=new google.maps.latlngparseffloatgpslat,parseFloatGPSlon;抱歉,代码块看起来很可怕,应该是:alertGPSlat;alertGPSlon;var myCenter=new google.maps.latlngparseffloatgpslat,parseFloatGPSlon;