如何将函数内部的坐标传递给变量,然后将它们添加到函数外部的另一个变量中

问题描述 投票:0回答:1

edit1:从showPosition函数下的lan和lon中删除了var。

我想获取我当前位置的坐标。获取这些坐标并通过函数传递它们。传递坐标后,我想将它们保存到变量lan和lon。使用这些坐标将它们添加到url变量。从那里我想使用该url变量来解析并使用JSON来查找名称,城市等。

HTML:

  <p>Click the button to get your coordinates.</p>

  <button onclick="getLocation()">Try It</button>
  <p id="geoLocal"></p>

JavaScript的:

 var lon, lan;
 var geoLocal = document.getElementById("geoLocal");

 //First part, I am creating the function to get the coordinates. 
 function getLocation() {
 if (navigator.geolocation) {
     navigator.geolocation.getCurrentPosition(showPosition);
 } else {
     geoLocal.innerHTML = "Geolocation is not supported by this browser.";
  }
};
 //The first lines of code outputs the function and tells me the coordinates. 
 //As it tells me these coordinates I want to save them to **lan** **and** lon variables. 

 function showPosition(position) {
 geoLocal.innerHTML = "Latitude: " + position.coords.latitude +
 "<br>Longitude: " + position.coords.longitude;
 lan= position.coords.latitude;
 lon= position.coords.longitude;
 console.log(lan, lon);
 return (lan, lon);

 };

上面我希望它返回lan和lon变量坐标,以便我可以将它们添加到下面的URL:

我想添加lan和lon并将它们添加到下面wUndergroundGeo的末尾。现在添加坐标,但基本上是url / variable

 var baseURL="http://api.wunderground.com/api/8a8af55ae16c8627/geolookup/q/"
 var addLanLon= baseURL+ lan + "," + lon; 
 var wUndergroundGeo=addLanLon;

wUndergroundGeo将替换下面的同一个变量。

 var wUndergroundGeo='http://api.wunderground.com/api/8a8af55ae16c8627/geolookup/q/34.4278,-119.7034111.json';
 var wUndergroundSF= 'http://api.wunderground.com/api/8a8af55ae16c8627/conditions/q/CA/San_Francisco.json'
 var weather= new XMLHttpRequest();
 weather.open("GET",wUndergroundGeo, false);
 weather.send(null);

 var myRequest= JSON.parse(weather.response);
 var cityLocal= "City location: "+ myRequest.location.city + "<br/>";
 document.getElementById("weather").innerHTML= cityLocal
javascript api coordinates weather weather-api
1个回答
0
投票

管理使其工作。我将其余的代码行移到了show position函数中。

 var lon, lan;
   var example='http://api.wunderground.com/api/8a8af55ae16c8627/geolookup/q/34.4277882,-119.7034209.json'; //Examples of completed coordinates
 var exampleCity="http://api.wunderground.com/api/8a8af55ae16c8627/conditions/q/CA/Santa_Barbara.json"; //example City.
 var geoLocal = document.getElementById("geoLocal");
 var temp= document.getElementById("temp");
 $(document).ready(function(){

 if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition);
 } else {
  geoLocal.innerHTML = "Geolocation is not supported by this browser.";
}
function showPosition(position) {
var beforeURL= 'http://api.wunderground.com/api/8a8af55ae16c8627/';
var conditionsURL="conditions/q/";
var geoLookUp="geolookup/q/"
var endURL=".json";
geoLocal.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
lan= position.coords.latitude;
lon= position.coords.longitude;
wUndergroundGeo=beforeURL + geoLookUp+ lan + "," + lon + endURL;
// return (position);

`

 var wUndergroundSF= 'http://api.wunderground.com/api/8a8af55ae16c8627/conditions/q/CA/San_Francisco.json'
 // var wUndergroundGeo= 'http://api.wunderground.com/api/8a8af55ae16c8627/geolookup/q/34.4278,-119.7034111.json';
 var weather= new XMLHttpRequest();
 weather.open("GET",wUndergroundGeo, false);
 weather.send(null);

 var myRequest= JSON.parse(weather.response);
 var cityLocal= "City location: "+ myRequest.location.city + "<br/>";
 document.getElementById("weather").innerHTML= cityLocal
 var stateURL= myRequest.location.state + "/";

 var cityURL=myRequest.location.city ;
 var cityURL=cityURL.replace(/ /gi,"_");//Replaces spaces of city with "_" so url will be valid.


 var weatherURL= beforeURL + conditionsURL +stateURL + cityURL + endURL;
 console.log("Weather", weatherURL)

 var weather2= new XMLHttpRequest();
 weather2.open("GET", weatherURL, false);
 weather2.send(null);

 var secRequest=JSON.parse(weather2.response);
 var fTemp= "Fahrenheit: " + secRequest.current_observation.temp_f;
 console.log(fTemp);

temp.innerHTML = fTemp;返回fTemp;

© www.soinside.com 2019 - 2024. All rights reserved.