【发布时间】:2015-11-16 01:31:31
【问题描述】:
我正在使用 Leafletjs 库为我的应用程序创建一个可视化销售模块。我可以使用 OnMapClick 事件添加标记,但不能使用来自服务的数据。
function onMapClick(e) {
var newPosition = e.latlng;
var field = GetFieldInfo();
if (field) {
if (bounds.contains(newPosition)) {
AddMarkerToMap(e.latlng.lat, e.latlng.lng, field);
}
}
}
在 AddMarkerToMap 函数中在地图上添加标记。
function AddMarkerToMap(_lat, _lng, field) {
var lat = JSON.parse(_lat);
var lng = JSON.parse(_lng);
var geojsonFeature = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [lat, lng]
}
}
var newMarker;
L.geoJson(geojsonFeature, {
pointToLayer: function (feature, latlng) {
newMarker = CreateMarker(lat, lng, field);
return newMarker;
}
}).addTo(map);
var lastValidPosition;
newMarker.on("drag", function (event) {
var latLng = newMarker.getLatLng();
if (bounds.contains(latLng)) {
lastValidPosition = latLng;
} else {
if (lastValidPosition) {
newMarker.setLatLng(lastValidPosition);
}
}
}, newMarker);
var popupContent = CreatePopupContent(field);
newMarker.bindPopup(popupContent);
newMarker.on("popupopen", onPopupOpen);
}
function CreateMarker(lat, lng, field) {
var position = [lat, lng];
var newMarker;
newMarker = new customFieldMarker(position, {
icon: L.icon({
iconUrl: 'Sources/css/images/Outside-Chartreuse.png',
iconSize: [48, 48], // size of the icon
iconAnchor: [24, 48], // point of the icon which will correspond to marker's location
popupAnchor: [0, -48],
}),
draggable: "true",
options: {
field: field
}
});
return newMarker;
}
在我的代码中,field 是一个来自 OData 服务的 JSON 对象,它包含以前保存的标记的 lat、lng 信息。为什么动态添加的标记没有显示在地图上?
【问题讨论】:
-
你为什么不从
pointToLayer回调函数加载lat lng,它给出了特征和几何,你应该从那里参考。 -
我认为问题是关于 geojsonFeature。因为我删除了 L.geoJson 块并添加了只有 addto(map) 的标记,所以它起作用了。你对此有什么想法吗? @Zhenyang
标签: javascript jquery leaflet