谷歌地图不接受事件

时间:2016-06-25 21:11:35

标签: javascript angularjs google-maps google-maps-api-3

我可以创建一个地图并设置标记但是当我尝试使用鼠标缩放或移动地图时没有任何反应。

这是我的功能

function initMap() {
var locations = $scope.locList ? $scope.locList : {loc: {loc: {lat: $scope.data.loc.lat, lng: $scope.data.loc.lng}}};
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
  zoom: $scope.locList ? 4 : 16,
  center: $scope.locList ? new google.maps.LatLng(39.833, -98.583) : new google.maps.LatLng($scope.data.loc.lat, $scope.data.loc.lng),
  scrollwheel: false
});
map.addListener('click', function(e) {
  map.set('scrollwheel', true);
});
map.addListener('mouseout', function(e) {
  map.set('scrollwheel', false);
});
var marker;
$.each(locations, function(key, value){
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(value.loc.lat, value.loc.lng),
    icon: $scope.data.marker,
    map: map
  });
})
}

和我的标记

<div id="map"></div>

这一切都发生在$scope.locList$scope.data填充后

1 个答案:

答案 0 :(得分:1)

基本上,您无法缩放,因为您使用scrollwheel: false初始化地图,并且在点击地图时尝试设置scrollwheel: true

相反,您应该在scrollwheel: true上附加mouseoverdraggable: true同样如此:

map.addListener('mouseover', function(e) {
   map.set('scrollwheel', true);
   map.set('draggable', true);
});

然后,在mouseout上,您再次将它们设为false:

map.addListener('mouseout', function(e) {
    map.set('scrollwheel', false);
    map.set('draggable', false);
});

这是您应该如何初始化地图:

var myLatLng = new google.maps.LatLng(41.9026329,12.452200400000038);
    var mapOptions = {
                        center: myLatLng,
                        zoom: 5,
                        scrollwheel: false,
                        draggable: false,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                     };

var map = new google.maps.Map(document.getElementById(attributes.id), mapOptions);
    var marker = new google.maps.Marker({
                                          position: myLatLng,
                                          map: map,
                                          title:"My town"
                 });

marker.setMap(map);

This is a working Plunker that reproduces your target

希望我能提供帮助。