2016-05-11 1 views
0

完全開示:私は、AngularJsの新機能であるJSとSUPERの新機能です。それで...AngularJsからGoogle Mapsオブジェクトを取得する適切な方法は何ですか?

AngularJsのルートプロバイダー機能を使用するWebアプリケーションを作成しようとしています。私は、データをバックエンドサーバから利用できるようになると、マーカなどを動的に追加できるGoogle Mapオブジェクトを含めるために使用しているtemplateURLの1つを希望します。

私が持っているどのような作業:

  • 複数のテンプレートのURLと偉大なチュートリアルに基づいて、それらの間の適切なナビゲーション(hereを見つけ
  • 私はui-gmap-google-map要素が含まれているとするためのコントローラ内部の別のテンプレートのURLを追加しました。テンプレートのURLは、私が(マップが表示されている)と呼ばれていることuiGmapGoogleMapApi.then経由でコールバックを追加

が欠落しているもの:。

  • 私は私はマーカーを追加することになっていますどこ基礎となるGoogleマップオブジェクトへのアクセスを得ることができない、ポリラインなど
  • 私が(参照HTMLマークアップの一部としてcontrolを含めるように述べているドキュメントhereを読んだことがあります私のmap.htmlファイル)と私の正確な質問に対処するように見えるthis promising answerを見た。しかし、私の$scope.map.controlオブジェクトは決して記入されません(詳細は下記を参照)。

    ページ/ map.html

    <div> 
        <ui-gmap-google-map center='map.center' zoom='map.zoom' control='map.control'></ui-gmap-google-map> 
    </div> 
    

    のindex.htmlをほとんどちょうど(:次のように

    webapp 
    --css 
        --style.css // contains ".angular-google-map-container {height: 400px;}" 
    --js 
        --angular-google-maps.min.js 
        --angular-simple-logger.js 
        --lodash.min.js 
    --pages 
        --about.html // from tutorial 
        --contact.html // from tutorial 
        --home.html // from tutorial 
        --map.html // my map is in here (see below) 
    --WEB-INF 
        --web.xml 
    --index.html // modified from the tutorial (see below) 
    --script.js // modified from the tutorial (see below) 
    

    私の新しい/変更されたファイルは、次のとおりです。次のよう

私のセットアップがありますチュートリアルコード)

<!DOCTYPE html> 
<html ng-app="scotchApp"> 
<head> 
    <!-- SCROLLS --> 
    <!-- load bootstrap and fontawesome via CDN --> 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" /> 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" /> 
    <link rel="stylesheet" href="css/style.css"/> 

    <!-- SPELLS --> 
    <!-- load angular and angular route via CDN --> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script> 
    <script src="js/lodash.min.js"></script> 
    <script src="js/angular-google-maps.min.js"></script> 
    <script src="js/angular-simple-logger.js"></script> 
    <script src="script.js"></script> 
</head> 
<body> 

<!-- HEADER AND NAVBAR --> 
<header> 
    <nav class="navbar navbar-default"> 
     <div class="container"> 
      <div class="navbar-header"> 
       <a class="navbar-brand" href="/">Angular Routing Example</a> 
      </div> 

      <ul class="nav navbar-nav navbar-right"> 
       <li><a href="#"><i class="fa fa-home"></i> Home</a></li> 
       <li><a href="#about"><i class="fa fa-shield"></i> About</a></li> 
       <li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li> 
       <li><a href="#map"><i class="my_location"></i> Map</a></li> 
      </ul> 
     </div> 
    </nav> 
</header> 

<!-- MAIN CONTENT AND INJECTED VIEWS --> 
<div id="main"> 

    <!-- angular templating --> 
    <!-- this is where content will be injected --> 
    <!--{{ message }}--> 
    <div ng-view></div> 

</div> 

</body> 
</html> 

そして最後には

// create the module and name it scotchApp 
var scotchApp = angular.module('scotchApp', ['ngRoute', 'uiGmapgoogle-maps']); 

scotchApp.config(function($routeProvider) { 
    $routeProvider   

     // ... 
     // Skipping unrelated routing code from the tutorial 
     // ... 

     // route for the map page (THIS IS NEW) 
     .when('/map', { 
      templateUrl : 'pages/map.html', 
      controller : 'mapController' 
     }); 
}).config(function (uiGmapGoogleMapApiProvider) { 
    uiGmapGoogleMapApiProvider.configure({ 
     key: '{KEY IS HERE}', 
     v: '3.20', 
     libraries: 'weather,geometry,visualization' 
    }); 
}); 

// ... 
// Skipping unrelated controller code from the tutorial 
// ... 

scotchApp.controller('mapController', function($scope, uiGmapGoogleMapApi) { 
    // Here is my empty "control" as specified by the documentation 
    $scope.map = { control: {}, center: { latitude: 45, longitude: -73 }, zoom: 8 }; 
    uiGmapGoogleMapApi.then(function(maps) { 
     console.log("Maps are ready"); 

     // Here $scope.map.control is still empty. What gives? 
     if ($scope) { 
      console.log("Scope is defined"); 
     } 
     else { 
      console.log("Scope is not defined"); 
     } 
    }); 
}); 

をscript.jsそれでは、私はその後、(ドキュメントあたり)マップを取得するために$scope.map.control.getGMap()を呼び出すことができるように私はコントロールを取得するために欠けていますか?

答えて

1

uiGmapGoogleMapApi約束が解決されたときに地図が準備完了であるとは限りません。代わりにuiGmapIsReady.promise()を試してください(詳細はthis SO answerをご確認ください)。作業コード:

scotchApp.controller('mapController', function($scope, uiGmapGoogleMapApi, uiGmapIsReady) { 
     // Here is my empty "control" as specified by the documentation 
     $scope.map = { control: {}, center: { latitude: 45, longitude: -73 }, zoom: 8 }; 

     uiGmapIsReady.promise().then(function(instances) { 
      console.log($scope.map.control.getGMap); //is set now 
     }); 
    }); 
関連する問題