Javascript 角度谷歌地图在$http之后调用

Javascript 角度谷歌地图在$http之后调用,javascript,angularjs,google-maps,Javascript,Angularjs,Google Maps,我需要在$http调用后显示一个带有db坐标的地图(带有承诺) 这是我的模板中的指令: <google-map center="map.center" draggable="true" refresh="true" control="map.control" data-tap-disabled="true" zoom="map.zoom"> <marker coords="marker.coords" options="marker.options" idkey="ma

我需要在$http调用后显示一个带有db坐标的地图(带有承诺)

这是我的模板中的指令:

<google-map center="map.center" draggable="true" refresh="true" control="map.control" data-tap-disabled="true" zoom="map.zoom">
    <marker coords="marker.coords" options="marker.options" idkey="marker.id" ></marker>
  </google-map>

promise中的数据,如id、姓名等,都能完美地显示出来。还有来自数据库的坐标。我插入测试只测试坐标的整数。如果我将$scope.map调用放在Friends.getDetail()之前,则映射会起作用。但这个电话是在回报一个承诺。你能帮忙吗?THX

对于这种情况,调用
uiGmapGoogleMapApi
作为
函数的一部分。然后调用
函数
在您的
$http
承诺返回后

下面是一个使用浏览器
navigator.geolocation
获取用户当前位置的示例-它可能需要几秒钟的时间(如果需要用户授权,则需要更多时间),因此通常比
$http
调用慢

我已将
getCurrentLocation()
服务包装在$q承诺中,以便控制器在加载映射之前等待当前位置返回。(参见小提琴中的代码)

同样的方法也适用于$http调用

myApp.controller('MainCtrl', function($scope, uiGmapGoogleMapApi, myAppServices) {
    $scope.myCurrentLocation = {};

    myAppServices.getCurrentLocation()
    .then(function(myCurrentLocation){
        $scope.myCurrentLocation = myCurrentLocation;
    })
    .then(function(){return uiGmapGoogleMapApi})    // map load call is here
    .then(function(maps){
        $scope.map = {
            center: {        // set on San Francisco as initial default
                latitude: 37.7749295, 
                longitude: -122.4194155 
            },
            zoom: 13,
            pan: 1,
            options: myAppServices.getMapOptions().mapOptions,
        };
        $scope.map.center = $scope.myCurrentLocation
    })
});
您可以在JSFIDLE中查看其余代码

注:
-我已经定义了
$scope.myCurrentLocation=myCurrentLocation
在它自己的“承诺”中(为了清楚起见),即使它不是真正的承诺-但您可以将任何同步任务与
uiGmapGoogleMapApi
(如下所示)放在同一个承诺函数中-只需确保将它们放在,并且您
返回
uiGmapGoogleMapApi
或者您将无法访问promise返回的
映射
对象:

myAppServices.getCurrentLocation()
.then(function(myCurrentLocation){
    $scope.myCurrentLocation = myCurrentLocation;
    return uiGmapGoogleMapApi})            // this is where the map load call is made
.then(function(maps){...
-我将
$scope.map.options
放在服务中也只是为了保持控制器整洁和可读性

-如果你想在控制器中定义
map.options
,你必须在最后一次
内完成。然后
承诺(即在地图加载后)或者你会得到
“google”是未定义的
对于
mapTypeId:google.maps.mapTypeId.ROADMAP>这样的定义类型错误,因为任何定义都是,在地图加载之前,管理员不知道什么是
google

你能创建一个小提琴吗?不幸的是,不能:(angular google maps的cdn现在已经关闭。本地获得了该插件。如果该插件再次启动,我将进行修改!这应该得到+10。该修改拥有让W3C地理定位与angular google maps一起工作所需的所有知识。)
myApp.controller('MainCtrl', function($scope, uiGmapGoogleMapApi, myAppServices) {
    $scope.myCurrentLocation = {};

    myAppServices.getCurrentLocation()
    .then(function(myCurrentLocation){
        $scope.myCurrentLocation = myCurrentLocation;
    })
    .then(function(){return uiGmapGoogleMapApi})    // map load call is here
    .then(function(maps){
        $scope.map = {
            center: {        // set on San Francisco as initial default
                latitude: 37.7749295, 
                longitude: -122.4194155 
            },
            zoom: 13,
            pan: 1,
            options: myAppServices.getMapOptions().mapOptions,
        };
        $scope.map.center = $scope.myCurrentLocation
    })
});
myAppServices.getCurrentLocation()
.then(function(myCurrentLocation){
    $scope.myCurrentLocation = myCurrentLocation;
    return uiGmapGoogleMapApi})            // this is where the map load call is made
.then(function(maps){...