javascript - Angular-google-maps:如何在标记上动态显示标题和描述

标签 javascript html angularjs angular-google-maps

我正在使用 Angular-google-maps,HTML 代码如下

  <ui-gmap-google-map center='mapData.map.center' zoom='mapData.map.zoom' 
  events="mapEvents">
    <ui-gmap-markers models="mapData.map.markers" coords="'self'">
    </ui-gmap-markers>
  </ui-gmap-google-map>

JS调用

angular.extend(this, $controller('MapsMixinController', 
{$scope:$scope, map:mapData.data[0].map}));

MapsMixinController 如下。从 js 代码调用此 Controller 。标记正在显示,点击即可标记。

MapsMixinController.js

/**
 * Controller providing common behaviour for the other map controllers
 */
angular
    .module('app')
    .controller('MapsMixinController', ['$scope', 'GeolocationService', 'uiGmapGoogleMapApi', 'map',
        function($scope, GeolocationService, GoogleMapApi, map) {
            var _this = this;

            $scope.mapEvents = {
                click: function(mapModel, eventName, originalEventArgs) {
                    var e = originalEventArgs[0];
                    if (e.latLng) {
                        $scope.mapData.map.markers.push({
                            id: new Date().getTime(),
                            latitude: e.latLng.lat(),
                            longitude: e.latLng.lng()
                        });
                        // This event is outside angular boundary, hence we need to call $apply here
                        $scope.$apply();
                    }
                }
            };

            // Returns a default map based on the position sent as parameter
            this.getDefaultMap = function(position) {
                return {
                    markers: [],
                    center: {
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude
                    },
                    zoom: 14
                };
            };

            // Initialize the google maps api and configure the map
            GoogleMapApi.then(function() {
                GeolocationService().then(function(position) {
                    $scope.mapData.map = map || _this.getDefaultMap(position);
                }, function() {
                    $scope.error = "Unable to set map data"; // TODO use translate
                });
            });
        }
    ]);

如何在鼠标悬停在标记上时显示标题?然后单击如何显示标记的描述?

最佳答案

您可以在创建标记数据时单独添加标题属性和纬度和经度属性。

    /**
 * Controller providing common behaviour for the other map controllers
 */
angular
    .module('app')
    .controller('MapsMixinController', ['$scope', 'GeolocationService', 'uiGmapGoogleMapApi', 'map',
        function($scope, GeolocationService, GoogleMapApi, map) {
            var _this = this;

            $scope.mapEvents = {
                click: function(mapModel, eventName, originalEventArgs) {
                    var e = originalEventArgs[0];
                    if (e.latLng) {
                        $scope.mapData.map.markers.push({
                            id: new Date().getTime(),
                            latitude: e.latLng.lat(),
                            longitude: e.latLng.lng(),
                            title: "Mouse over text"
                        });
                        // This event is outside angular boundary, hence we need to call $apply here
                        $scope.$apply();
                    }
                }
            };

            // Returns a default map based on the position sent as parameter
            this.getDefaultMap = function(position) {
                return {
                    markers: [],
                    center: {
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude
                    },
                    zoom: 14
                };
            };

            // Initialize the google maps api and configure the map
            GoogleMapApi.then(function() {
                GeolocationService().then(function(position) {
                    $scope.mapData.map = map || _this.getDefaultMap(position);
                }, function() {
                    $scope.error = "Unable to set map data"; // TODO use translate
                });
            });
        }
    ]);

关于javascript - Angular-google-maps:如何在标记上动态显示标题和描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30643481/

相关文章:

html - 倾斜/倾斜的表头

javascript - Angular 2中的多个复选框

javascript - 如何动态更新 AngularJS 谷歌地图?

javascript - 切换到使用 Browserify 后,Angular UI Router 的 $stateChangeSuccess 未发出

javascript - AngularJs - "deferred"或 "batched"指令

javascript - 在 CodeMirror 中查找 token 的父元素

html - 当父级小于子级时,将 div 在其父级中水平居中

javascript - Highcharts - 更改类别轴上的组填充和列宽度

javascript - 如何使滚动条长度小于父 block ?

HTML/CSS 帮助使链接悬停填充整个导航栏