JavaScript ES6返回undefined尽管已设置

时间:2018-07-09 06:59:00

标签: javascript

我有一堂课来处理地理信息和谷歌地图服务:

class MapService {

    constructor(apiKey) {
        this.apiKey = apiKey;
        this.googleMapsClient = require('@google/maps').createClient({
            key: this.apiKey
        });
    }

    getLocationFromGeoData(){
        this.coords = this.getCoordinatesFromGeoData();
        console.log(this.coords); // => undefined
        return this.coords;
    }

    getCoordinatesFromGeoData(){
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(function(position){
                let coords = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };
                console.log(coords); // shows object with coordinates
                return coords;
            })
        } else {
            console.warn("Geolocation wird nicht unterstützt!");
        }
    }
}

export {MapService}

我的导入:

import {MapService} from './modules/MapServices';
let googleMapsApiKey = document.head.querySelector('meta[name="google-maps-api-key"]');
window.MapService = new MapService(googleMapsApiKey.content);

现在在我的脚本(Vue.js)中,我将此类方法称为getLocationFromGeoData

var coords = MapService.getLocationFromGeoData();
console.log(coords); // => undefined

getCoordinatesFromGeoData之外,当我尝试访问坐标对象时都会得到undefined

从导入的类返回值时,我需要照顾一些特殊的东西吗?

0 个答案:

没有答案