0
총 JS Newb에서 간단한 JS 코드를 구조화하려고합니다. 목표는 사용자 위치를 얻는 것입니다.구조 JS - 변수가 비어 있습니까?
작동하지만 getUserCoordinates 함수가 값을 반환하지 않습니까? 나는 초심자에게서 논리적 인 실패 일 뿐이라고 생각한다.
var app = {
geocoder: null,
init: function() {
this.geocoder = new google.maps.Geocoder();
coordinates = this.getUserCoordinates();
console.log(coordinates); // empty??
return;
},
getUserCoordinates: function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (coords) {
console.log(coords); // works Geocoder Object!
return coords; // the problem
}, // more code
논리 감사합니다, 거기에 어떤 좋은 해결책이 있습니까? – opHASnoNAME
콜백 함수 (getUserCoordinates 메소드에 전달)를 사용할 수도 있고 값을 나타내는 콜백을 사용하는 함수에서 [Promise] (http://wiki.commonjs.org/wiki/Promises)를 반환 할 수도 있습니다 내부적으로. – Bergi