javascript - Angular Google Maps map 点击事件工作一次

标签 javascript angularjs google-maps angular-google-maps

我一直遇到与 this 非常相似的问题

然而,那个人的问题从未得到回答,我的情况略有不同。

我正在尝试向我的 map 添加一个点击事件,该事件会将 map 的中心更改为点击的位置。我执行此操作的代码效果很好,但它只能运行一次,然后当我再次单击时出现此错误:

angular-google-maps.js:6815 未捕获类型错误:无法读取未定义的属性“click”

这是我的 map 对象:

vm.map = {
     center: {
          latitude: l.latitude,
          longitude: l.longitude
     },
     zoom: 13,
     events: {
          click: function(map, event, coords) {
               vm.map = {
                    center: {
                         latitude: coords[0].latLng.lat(),
                         longitude: coords[0].latLng.lng()
                    },
               };

               $scope.apply();

           }
     }
};

这是我的 html:

<ui-gmap-google-map center="location.map.center" zoom="location.map.zoom" draggable="true" options="tabLocations.map.options" events=location.map.events>
    <ui-gmap-search-box template="'scripts/components/tab-locations/searchbox.tpl.html'" events="location.map.searchbox.events" parentdiv="'searchbox-container'"></ui-gmap-search-box>
    <ui-gmap-marker ng-if="location.active" idKey="1" coords="location" options="{draggable: false, icon: 'images/icons/gps-marker-active.svg'}"></ui-gmap-marker>
    <ui-gmap-marker ng-if="!location.active" idKey="1" coords="location" options="{draggable: false, icon: 'images/icons/gps-marker-inactive.svg'}"></ui-gmap-marker>
</ui-gmap-google-map>

最佳答案

第一次点击后,您将重新定义一个没有点击事件的全新 map :

vm.map = {
    center: {
        latitude: coords[0].latLng.lat(),
        longitude: coords[0].latLng.lng()
    },
};

这将覆盖您之前设置的所有属性,包括click

改用setCenter:

click: function(map, event, coords) {

    var latLng = new google.maps.LatLng(latitude: coords[0].latLng.lat(), latitude: coords[0].latLng.lng());
    vm.map.setCenter(latLng);

}

引用:Google Maps API

关于javascript - Angular Google Maps map 点击事件工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38061881/

相关文章:

javascript - 在 JavaScript 中更新 'Enable' 的 'asp:DropDownList'

JavaScript 如何定义一个未定义长度的空对象数组?

javascript - 在谷歌地图多边形数组上设置点击优先级

google-maps - 恢复已删除的 Google 公共(public) API key

c# - GMap.Net 在控件中不显示完整 map

javascript - Angular-App 如何引用其依赖项?

javascript - 作为一个整体解析具有 promise 属性的对象的更好方法?

javascript - 无法添加第二个 ng-controller

angularjs - $window.open 在 Firefox 上抛出 $apply 错误

javascript - angularjs:禁用单选按钮,但实际上并未禁用它