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

时间:2017-12-17 02:49:24

标签: javascript api coordinates weather weather-api

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

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

HTML:            

     

 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);

 };

JavaScript的:

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

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

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

 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

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

builder.Append("<link rel='stylesheet' type='text/css' href='~/PDF/style.css'>");

1 个答案:

答案 0 :(得分: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;