我是竞赛团队的一员,我们正在尝试获取用户位置,并从该输出中获取其位置是否适合某种能源。我真正需要的只是一种方式:
我对JavaScript仍然相当陌生,对我的问题,或者我错过的任何其他问题的任何帮助,或者我做错的更好的编程习惯都会有所帮助。我将包含所有代码,以便您可以看到我的目标,但我认为只有两种方法需要一些工作。非常感谢。
P.S。如果我的帖子有问题,我很抱歉,这是我第一次发帖到论坛。
var userLocation=document.getElementById("location");
var latitude1;
var longitude1;
//Program gets user location and divides US into 32 sections, and based on the location sets and outputs if the specefic energy type is good for the users location
window.onload = function () {
if (navigator.geolocation) {
setLat()
setLon()
displayEnergy()
}
else
userLocation.innerHTML = "GeoLocation is not supported by this browser";
}
function setLat() {
function displayLocation(position) {
latitude1 = position.coords.latitude;
}
navigator.geolocation.getCurrentPosition(displayLocation);
}
function setLon() {
function displayLocation(position) {
longitude1 = position.coords.longitude;
}
navigator.geolocation.getCurrentPosition(displayLocation);
}
function getSection() {
var lat, lon;
if (longitude1<=-65&&longitude1>-75)
lat="a";
else if (longitude1<=-75&&longitude1>-85)
lat="b";
else if (longitude1<=-85&&longitude1>-95)
lat="c";
else if (longitude1<=-95&&longitude1>-105)
lat="d";
else if (longitude1<=-105&&longitude1>-115)
lat="e";
else if (longitude1<=-115&&longitude1>-125)
lat="f";
if (latitude1>=25&&latitude1<30)
lon="a";
else if (latitude1>=30&&latitude1<35)
lon="b";
else if (latitude1>=35&&latitude1<40)
lon="c";
else if (latitude1>=40&&latitude1<45)
lon="d";
else if (latitude1>=45&&latitude1<50)
lon="e";
//Alaska and Hawaii
else if (latitude1>=50&&latitude1<75&&longitude1<=-140&&longitude1>-170){
lat="g";
lon="g";
}
else if(latitude1>=10&&latitude1<30&&longitude1<=-150&&longitude1>-165){
lat="h";
lon="h";
}
var section=lon+lat;
return section;
}
function pageIs() {
//What energy is the current page displaying, utilizes empty div on each page
var page;
if (document.getElementById("pageSolar"))
page="solar";
else if(document.getElementById("pageWind"))
page="wind";
else if(document.getElementById("pageGeo"))
page="geo";
else if(document.getElementById("pageHydro"))
page="hydro";
return page;
}
function displayEnergy() {
var solarE, windE, geoE, hydroE;
//////////////////////////////////////////////////////////////////////////////////
//Is solar energy a good choice
if (getSection()=="ba" ||
getSection()=="bb" ||
getSection()=="bc" ||
getSection()=="ca" ||
getSection()=="cb" ||
getSection()=="cc" ||
getSection()=="da" ||
getSection()=="db" ||
getSection()=="dc" ||
getSection()=="dd" ||
getSection()=="eb" ||
getSection()=="ec" ||
getSection()=="fb" ||
getSection()=="fc" ||
getSection()=="hh")
solarE=true;
else
solarE=false;
//Is wind energy a good choice
if (getSection()=="ad" ||
getSection()=="ba" ||
getSection()=="bb" ||
getSection()=="bd" ||
getSection()=="cb" ||
getSection()=="cd" ||
getSection()=="ce" ||
getSection()=="da" ||
getSection()=="db" ||
getSection()=="fb" ||
getSection()=="fc" ||
getSection()=="fd" ||
getSection()=="fe")
windE=true;
else
windE=false;
//Is geothermal energy a good choice
if (getSection()=="db" ||
getSection()=="dc" ||
getSection()=="dd" ||
getSection()=="de" ||
getSection()=="eb" ||
getSection()=="ed" ||
getSection()=="ee" ||
getSection()=="fb" ||
getSection()=="fc" ||
getSection()=="hh")
geoE=true;
else
geoE=false;
//Is hydroelectric energy a good choice
if (getSection()=="ad" ||
getSection()=="ba" ||
getSection()=="bb" ||
getSection()=="bc" ||
getSection()=="bd" ||
getSection()=="cb" ||
getSection()=="cd" ||
getSection()=="ce" ||
getSection()=="da" ||
getSection()=="ec" ||
getSection()=="ed" ||
getSection()=="fb" ||
getSection()=="fc" ||
getSection()=="fd" ||
getSection()=="gg" ||
getSection()=="hh")
hydroE=true;
else
hydroE=false;
////////////////////////////////////////////////////////////////////////////////
if (pageIs()=="solar"&&solarE===true)
userLocation.innerHTML="Based on your location, solar energy would be a good option";
else if (pageIs()=="solar"&&solarE===false)
userLocation.innerHTML="Based on your location, solar energy would not be a good option";
else if (pageIs()=="wind"&&windE===true)
userLocation.innerHTML="Based on your location, wind energy would be a good option";
else if (pageIs()=="wind"&&windE===false)
userLocation.innerHTML="Based on your location, wind energy would not be a good option";
else if(pageIs()=="geo"&&geoE===true)
userLocation.innerHTML="Based on your location, geothermal energy would be a good option";
else if(pageIs()=="geo"&&geoE===false)
userLocation.innerHTML="Based on your location, geothermal energy would not be a good option";
else if(pageIs()=="hydro"&&hydroE===true)
userLocation.innerHTML="Based on your location, hydroelectric energy would be a good option";
else if(pageIs()=="hydro"&&hydroE===false)
userLocation.innerHTML="Based on your location, hydroelectric energy would not be a good option";
}