React & React Native
HTML5 Geolocation Async 처리 [Geolocation 비동기처리 ]
클라인STR
2019. 1. 21. 22:43
geolocation 을 이용하여 현재 위치 (위도, 경도를 구하는 코드이다.) 해당코드를 이용하여 위도, 경도 위치 정보를 파라메터로 하여 API를 호출하는 서비스를 개발하는 중인데 비동기 호출때문에 문제가 됬다.
1 2 3 4 5 6 7 8 9 10 11 12 | navigator.geolocation.getCurrentPosition( (position) => { this.setState({ latitude: position.coords.latitude, longitude: position.coords.longitude, error: null, }) }, (error) => this.setState({ error: error.message }), { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }, ); | cs |
[기존에 썼던 코드]
1 2 3 4 5 6 7 8 9 10 11 12 13 | var getPosition = function (options) { return new Promise(function (resolve, reject) { navigator.geolocation.getCurrentPosition(resolve, reject, options); }); } getPosition() .then((position) => { console.log(position); }) .catch((err) => { console.error(err.message); }); | cs |
[변경될 형태의 코드]
참고
https://gist.github.com/varmais/74586ec1854fe288d393
https://stackoverflow.com/questions/47544564/how-geolocation-getcurrentposition-return-value