做一個圖標和表單關聯的例子,點擊圖片可以彈出表單顯示圖片詳細信息,默認表單是關閉的,可以手動關閉表單.
首先是JS定義圖標,包括名字、經緯度、圖片等信息
markers: [
{
name: 'marker A',
lat: 39,
lng: 116,
draggable: true,
icon: icons.green
}
]
JS圖片的定義,定義圖片路徑、大小等信息
var icons = {
green: {
iconUrl: 'img/leaf-green.png',
iconSize: [30, 30],
className: 'green',
iconAnchor: [15, 15]
},
}
默認表單爲關閉狀態
$scope.alarmForm = false;
點擊圖片打開表單
$scope.$on('leafletDirectiveMarker.click', function(e, args) {
console.log("Leaflet Click");
var i = args.modelName;
$scope.alarmForm = true;
$scope.modelName = $scope.markers[i].name;
$scope.modelLat = $scope.markers[i].lat;
$scope.modelLng = $scope.markers[i].lng;
$scope.modelLayer = $scope.markers[i].layer;
$scope.marker = $scope.markers[i];
});
關閉表單,把此函數通過ng-click綁定到表單關閉按鈕
$scope.cancel = function() {
$scope.alarmForm = false;
};
HTML表單
<form role="form" ng-show="alarmForm" class="form-horizontal" >
<div class="col-sm-12">
<br>
名字:<input ng-model="modelName">
經度:<input ng-model="modelLat">
緯度:<input ng-model="modelLng">
層名:<input ng-model="modelLayer">
位置:<input ng-model="cen.lat">
位置:<input ng-model="cen.lng">
層級:<input ng-model="cen.zoom">
圖片:<input ng-model="iconUrl">
</div〉
</form>