2017-05-08 9 views
1

저는 자바 스크립트가 제 경험이 제한적이라고 생각합니다. 내가 구글로 Node.js를 클라이언트 라이브러리를 사용하고 , 여기 - https://github.com/googlemaps/google-maps-services-js 예는 gecode 요청을 실행하고 결과를 인쇄하는 방법을 다음node.js Google지도 클라이언트 라이브러리 사용

var googleMapsClient = require('@google/maps').createClient({ 
    key: 'your API key here' 
}); 

클라이언트 객체를 생성하는 방법을 보여줍니다

// Geocode an address. 
googleMapsClient.geocode({ 
    address: '1600 Amphitheatre Parkway, Mountain View, CA' 
}, function(err, response) { 
    if (!err) { 
    console.log(response.json.results); 
    } 
}); 

내가해야할 일은 파일에서 주소 목록을 읽고 모든 파일에 대한 개체 배열을 작성하는 것입니다.

나는 내 코드의 라인을 따라 뭔가를하고 싶지 :

create address-objects array 
create a client object 
open the text files 
for each line in text files 
geocode the line(address) 
add the address and the results into an object and add it to the address-objects 

나는 그래 난 그것을 어떻게 액세스합니까 경우 googleMapsClient.geocode이 뭔가를 반환 여부를 이해하지? 사전 조나단에서 나는 분명했다

var gc_results = googleMapsClient.geocode(param , ...) 

희망, 감사 : 나는의 라인을 따라 몇 가지 변수를 설정해야합니다.

답변

1

callback 함수 내에서 응답에 액세스 할 수 있습니다. 각 사용자가 파일 나갈 주소

var gc_results = []; 

그런 다음 함수를 호출한다 각 주소에 대해 호출 할 수있는이 기능을 갖고 싶어

그래서 먼저 하늘의 배열을 정의 할 수 있습니다 이 비슷한 :이 후

addresses.forEach(function(){ 
    googleMapsClient.geocode({ 
     address: 
    }, function(err, res) { 
     gc_results.push(res.json.results); 
    }) 
}) 

, 당신은

필요로하는 정보 전체 gc_results 배열을해야합니다