2017-12-17 35 views
0

edit1:showPosition関数の下でlanとlonからvarを削除しました。関数内から変数に座標を渡して、関数の外にある変数を別の変数に追加する方法

現在地の座標を取得しようとしています。 これらの座標を取得し、関数に渡します。 座標を渡した後、それらを変数lanとlonに保存します。 これらの座標を使用して、それらをurl変数に追加します。

など、都市、私は名前を見つけるためにJSONを解析し、使用するためにそのURL変数を使用したい。そこから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 /変数が

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 
+0

これまでに何を試しましたか?いつあなたはapiに電話するつもりですか? –

+0

一見すると、 'showPosition'関数でそれらを再宣言することで、グローバル変数をシャドーイングしています。 –

+0

私が探している結果は、座標を取得し、後で@CarlEdwardsで使用できる変数に保存することです。 – GauchoRoger

答えて

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。 return fTemp;