2016-08-21 4 views
0

지금 사용자의 위치 정보에 크게 의존하는 앱을 만들고 있습니다. 현재이 사용자의 위도와 경도를 통해서만 사용자의 주소를 알아야합니다. 이를 위해 나는 mdg:geolocation (위도와 경도를 얻기 위해)과 meteor-google-reverse-geocode (위도와 경도를 주소로 변환)의 두 패키지를 사용합니다. 나는 다른 매개 변수들 사이에서 사용자의 위치를 ​​표시하지만, 처음에는 등록에서이 location PARAM 내가 세션을 구현하는 것 (의 로그를 받고되어야 User 구성 요소가유성 앱에서 사용자의 위치 정보 가져 오기

import React, { Component, PropTypes } from 'react'; 
import { Meteor } from 'meteor/meteor'; 
import { Accounts } from 'meteor/accounts-base'; 
import { Geolocation } from 'meteor/mdg:geolocation'; 
import { reverseGeocode } from 'meteor/jaymc:google-reverse-geocode'; 

export default class Profile extends Component { 
    setLocation() { 
     var latLng = Geolocation.latLng(); 
     var lat = latLng.lat; 
     var lng = latLng.lng; 
     reverseGeocode.getSecureLocation(lat, lng, function(location) { 
      Meteor.users.update(Meteor.userId(), { 
       $set: {"profile.location": reverseGeocode.getAddrStr()} 
      }); 
     }); 
    } 
    render() { 
     return (
      <div className="container"> 
       <div className="row"> 
        <div className="col s4"> 
         <User /> 
        </div> 
        <button onClick={this.setLocation.bind(this)}>Set location</button> 
       </div> 
    </div> 
     ) 
    } 
} 

: 는 지금은 다음과 같은 코드가 있습니다 나중에), 트릭을 수행하는 버튼을 만들었지 만 두 번 클릭 할 때만 만들었습니다. 첫 번째 클릭으로 콘솔에서 오류를 표시하는 대신 아무 정보도 표시되지 않지만 두 번째로 눌렀을 때 원하는대로 작동합니다. 즉 올바른 주소를 얻은 다음 내 User 구성 요소에 표시합니다. 좋아요, 데이터를 가져올 수는 있지만 두 번 클릭하면 허용되지 않습니다. 등록 또는 로그인 한 후이 데이터를 가져오고 싶습니다. 즉, 두 번째가 아닌 첫 번째 클릭만으로도이 데이터를 가져오고 싶지만 여전히 방법을 모릅니다. 그것을하기 위해. 어떤 도움이나 생각이라도 감사 할 것입니다. 설명 된 문제에 전념해 주셔서 감사합니다!

UPD

몇 가지 조사 후, 나는 그 때 수동으로 계산을 중지 Geolocation 기능은 반응하지 않고는 데이터를로드하는 데 시간이 걸릴 수 있습니다, 그래서 그것을 반응성하기 위해 ReactiveVar을 사용 Tracker 발견 완료되었습니다. 이제 내 코드는 다음과 같습니다

setLocation() { 
    var latLng = new ReactiveVar(); 
    Tracker.autorun(function(computation) { 
     latLng.set(Geolocation.latLng()); 
     if (latLng.get()) { 
      computation.stop(); 
      console.log(latLng); 
      var lat = latLng.lat; 
      var lng = latLng.lng; 
      reverseGeocode.getSecureLocation(lat, lng, function(location) { 
       Meteor.users.update(Meteor.userId(), { 
        $set: {"profile.location": reverseGeocode.getAddrStr()} 
       }); 
      }); 
     } 
    }) 
} 

google-reverse-geocode.js 파일에 다른 오류 Uncaught TypeError: Cannot read property 'formatted_address' of undefined 먼저 클릭으로 lanlng 특성을 계산하지만 여기에 온다.

답변

1

좋아,이 문제를 해결했습니다. 나는 ReactiveVar 출력을보고 내 실수를 깨달았다. 출력은 포함하고 curValue 개체는 lanlng 속성이 저장되어 있으므로 이러한 속성을 추출하려면 latLng.curValue.lat과 같이 명시 적으로 호출해야합니다. 다음은 첫 번째 게시물에서 설명한대로 작동하는 고정 된 기능입니다.

setLocation() { 
    var latLng = new ReactiveVar(); 
    Tracker.autorun(function(computation) { 
     latLng.set(Geolocation.latLng()); 
     if (latLng.get()) { 
      computation.stop(); 
      console.log(latLng); 
      var lat = latLng.curValue.lat; 
      var lng = latLng.curValue.lng; 
      reverseGeocode.getSecureLocation(lat, lng, function(location) { 
       Meteor.users.update(Meteor.userId(), { 
        $set: {"profile.location": reverseGeocode.getAddrStr()} 
       }); 
      }); 
     } 
    }) 
} 

희망이 있으면 도움이됩니다.

관련 문제