Initialize Google Map in AngularJS

后端 未结 4 1162
一个人的身影
一个人的身影 2020-12-07 19:17

I am considering migrating from backbonejs to angularjs.

In backbone I am able to initialize a view at which point I create an instance of google map. I can then pa

4条回答
  •  悲哀的现实
    2020-12-07 19:35

    Here's my solution for using the maps api with Angularjs routeProvider: in your index you have to add:

    angular-google-maps.min.js and lodash.min.js

    in application.js:

    (function() {
    
    var app = angular.module("myApp", ["ngRoute", "uiGmapgoogle-maps"]);
    
    
    app.config(function(uiGmapGoogleMapApiProvider) {
        uiGmapGoogleMapApiProvider.configure({
            key: 'YOUR KEY HERE',
            v: '3.17',
            libraries: 'weather,geometry,visualization'
        });
    });
    
    app.config(function($routeProvider) {
        $routeProvider
            .when("/home", {
                templateUrl: "home.html",
                controller: "HomeController"
            })
            .when("/workwith", {
                templateUrl: "workwith.html",
                controller: "WorkwithController"
            })
    
    
    
    
        .otherwise({
            redirectTo: "/home"
        });
    
    });
    

    })();

    Last But not least in your controller:

    (function() {
    var app = angular.module("myApp");
    var MyController = function($scope, $http, $log, $location, $routeParams, uiGmapGoogleMapApi) {
        $log.info("MyController");
        $log.info($routeParams);
    
        // Define variables for our Map object
        var areaLat = 44.2126995,
            areaLng = -100.2471641,
            areaZoom = 3;
    
        uiGmapGoogleMapApi.then(function(maps) {
            $scope.map = {
                center: {
                    latitude: areaLat,
                    longitude: areaLng
                },
                zoom: areaZoom
            };
            $scope.options = {
                scrollwheel: false
            };
        });    
     };
    app.controller("MyController", MyController);
    

    })();

提交回复
热议问题