Angular-leaflet-directive自定义HTML弹出窗口

时间:2015-05-06 02:17:19

标签: javascript angularjs popup leaflet angular-leaflet-directive

我尝试使用angular-leaflet-directive创建自定义弹出窗口。我打开了leaflet.draw on:create event的弹出窗口。这里:

map.on('draw:created', function(e) {

    layer = e.layer;
    drawnItems.addLayer(layer);
    var newComment = "Enter Comment";
    var newID = "ID";
    var newGeoJsonFeat = layer.toGeoJSON();
    newGeoJsonFeat.properties = {"comment":newComment, "id":newID};
    console.log(newGeoJsonFeat);

    onEachFeature(newGeoJsonFeat,layer);
    layer.openPopup();
});

然后我使用@ blackjid的逻辑,如下所示:https://github.com/tombatossals/angular-leaflet-directive/issues/238绑定自定义弹出窗口

function onEachFeature(feature, layer) {
    // Create get the view template
    var popupContent = '<div ng-include="\'partials/popup_newfeat.html\'"></div>';
    console.log("assigning popup");

    // Bind the popup
    // HACK: I have added the stream in the popup options
    layer.bindPopup(popupContent,{
      closeButton: false,
      maxHeight: 300,
      feature: feature
    });
};

$scope.$on('leafletDirectiveMap.popupopen', function(event, leafletEvent){

  // Create the popup view when is opened
  var feature = leafletEvent.leafletEvent.popup.options.feature;

  var newScope = $scope.$new();
  newScope.stream = feature;

  $compile(leafletEvent.leafletEvent.popup._contentNode)(newScope);
});

长话短说,一切正常,但弹出容器没有正确调整大小以适应新内容。高度似乎正确,但宽度不对。

我尝试使用:

.leaflet-popup-content {
    width:auto !important;
}

这可能就足够了,但这会导致弹出式锚点由于某种原因转移到弹出窗口的左下角。单击靠近地图顶部时,AutoPan也会中断。

有谁知道我可以在哪里以及如何让popup.update()开火?我相信这需要发生什么,但我不知道在哪里实施它。我试过在layer.openPopup()之后调用它,如下所示:

    onEachFeature(newGeoJsonFeat,layer);
    layer.openPopup();
    layer.getPopup().update();
});

但这似乎没有做任何事情。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

您需要使用'leafletEvent'。试试这个:

myApp.controller('YourController', ['$scope', 'leafletEvent', function($scope) {

    // after all your crazy custom popup stuff ...
    leafletData.getMap().then(function(map) {
        layer.getPopup().update();
    });
}]);

答案 1 :(得分:0)

我最终将图像宽度存储在GeoJson的属性中,然后将minWidth设置为bindPopup函数中的该值。

layer.bindPopup(popupContent,{
  closeButton: true,
  closeOnClick: false,
  minWidth: feature.properties.width,
  autoPanPadding: L.point(20,20),
  keepInView: false,
  feature: feature
});