2017-12-17 37 views
0

edit1 : showPosition 함수 아래에서 lan과 lon에서 var를 제거했습니다.함수 내부에서 변수로 좌표를 전달한 다음 함수 외부에서 다른 변수에 추가하는 방법

현재 위치의 좌표를 가져 오려고합니다. 좌표를 가져 와서 함수를 통해 전달하십시오. 좌표를 전달한 후 변수 lan 및 lon에 저장하고 싶습니다. 이러한 좌표를 사용하여 URL 변수에 추가하십시오.

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

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

자바 스크립트 :

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

}; 
거기에서 나는 등 이름, 도시,

HTML을 찾아 분석하고 JSON을 사용하여 해당 URL 변수를 사용하려면

위의 코드는 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

쇼 코드 위치 함수 내에서 나머지 코드 행을 옮겼습니다.

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;