Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果使用.then()或其他方法(JavaScript、Maps API),则从另一个函数中的嵌套函数返回值_Javascript_Angularjs_Google Maps_Nested Function_Yandex Maps - Fatal编程技术网

如果使用.then()或其他方法(JavaScript、Maps API),则从另一个函数中的嵌套函数返回值

如果使用.then()或其他方法(JavaScript、Maps API),则从另一个函数中的嵌套函数返回值,javascript,angularjs,google-maps,nested-function,yandex-maps,Javascript,Angularjs,Google Maps,Nested Function,Yandex Maps,我编写了AngularJS应用程序,其中显示了从JSON对象获取的特定坐标的Google、Yandex和Gis地图。JSON可能包含[0…n]个对象。 它可以很好地与GoogleMapsAPI配合使用,但不能与Yandex/Gis地图API配合使用 Yandex地图: // DOESN'T WORK $scope.yandexMaps = function (city) { ymaps.ready(init); function init() { $scope.m

我编写了AngularJS应用程序,其中显示了从JSON对象获取的特定坐标的Google、Yandex和Gis地图。JSON可能包含[0…n]个对象。 它可以很好地与GoogleMapsAPI配合使用,但不能与Yandex/Gis地图API配合使用

Yandex地图:

// DOESN'T WORK
$scope.yandexMaps = function (city) {
    ymaps.ready(init);
    function init() {
        $scope.mapContainer = document.createElement('div');
        $scope.mapContainer.className = 'mapCon';
        $scope.yandexMap = new ymaps.Map($scope.mapContainer, {
            center: [city.lat, city.lng],
            zoom: 12
        });

        $scope.placemark = new ymaps.Placemark([city.lat, city.lng]);
        $scope.yandexMap.geoObjects.add($scope.placemark);
        return $scope.mapContainer; //I NEED TO GET IT !
    }
    //BUT I NEED TO GET IT THERE !
};
地理信息系统地图:

// DOESN'T WORK
$scope.gisMaps = function (city) {
    DG.then(init);
    function init() {
        $scope.mapContainer = document.createElement('div');
        $scope.mapContainer.className = 'mapCon';

        $scope.gisMap = DG.map($scope.mapContainer, {
            center: [city.lat, city.lng],
            zoom: 13
        });

        DG.marker([city.lat, city.lng]).addTo($scope.gisMap);
        return $scope.mapContainer; //I NEED TO GET IT !
    }
    //BUT I NEED TO GET IT THERE !
};
问题是
ymap.ready(init)
DG.then(init)仅在检查函数的就绪映射状态后调用init()函数。在这两种情况下,我都无法返回
$scope.mapContainer
,因为它们位于嵌套函数中

我试图在init()函数上方创建全局变量并将其赋值给此
$scope.mapContainer
,但当我在init()函数下方的父级中返回此全局值时,它不可见。它仅在嵌套函数中可见

您可以在这里看到正在工作的JSFIDLE:
请打开控制台查看错误

如果您在代码末尾对此部分进行注释:

这是:

你会看到完美的谷歌地图。我希望看到Yandex和Gis地图完全相同。请帮忙


yandexMaps函数具有基于回调的API。gisMaps函数有一个基于承诺的API。将问题分为两个问题。JavaScript是单线程的。函数只能返回立即可用的数据或将来解析为值的挂起承诺。
        //adding yandex map to common container
        $scope.yandexMap = $scope.yandexMap(value);
        $scope.yandexMap.id = "yandexMapContainer" + i;
        angular.element($scope.container).append($scope.yandexMap);
        //adding gis map to common container
        $scope.gisMap = $scope.gisMaps(value);
        $scope.gisMap.id = "gisMapContainer" + i;
        angular.element($scope.container).append($scope.gisMap);