Leaflet中的L.geoJSON一个坑

 最近在使用 SuperMap iClient 9D for Leaflet 中的 L.supermap.queryService(url).queryByGeometry()接口 来定位某个建筑图斑时,发现2个问题。

1、查不出来

2、查出来了,却显示不出来

具体的说下:

L.supermap.tiledMapLayer(url, {
    transparent: true,
    maxZoom: 24,
    minZoom: 13
}).addTo(map);

把iserver中的地图加载,地图是3857的坐标系。

点击map时,触发查询。

map.on("click", function (e) {
    identifyBuilding(e);
});

e中有坐标信息,获取出来的是经纬度坐标。

var latlng = e.latlng; 
var point = L.point(latlng.lon, latlng.lat);
var param = new SuperMap.QueryByGeometryParameters({
        queryParams: {name: "建筑@manager"},
        geometry: point,
        expectCount: 1,
        networkType: SuperMap.GeometryType.REGION,
        queryOption: SuperMap.QueryOption.ATTRIBUTEANDGEOMETRY,
        spatialQueryMode: SuperMap.SpatialQueryMode.INTERSECT
});
L.supermap.queryService(url).queryByGeometry(param, function (serviceResult) {....});

结果没有返回。。。悲剧了。。。怎么设置查询参数都没有用。。。

前后试了快1个小时,最终发现,几何查询的对象所使用的坐标系要和地图坐标系保持一致。。。

遂改point

 var point = L.CRS.EPSG3857.project(L.latLng(latlng.lat, latlng.lng)); 

把经纬度改为大地坐标,用 L.CRS.EPSG3857.project 静态方法把4326的坐标转为3857的坐标。。。

再查询,结果正常。。。但是使用L.geojson把查询出来的要素显示出来时又不出来了。。。

继续悲剧了。。。

 var result = serviceResult.result;
 L.geoJSON(result.recordsets[0].features).addTo(map);

继续查询文档,发现有个coordsToLatLng参数,Function that will be used for converting GeoJSON coordinates to LatLngs. The default is the coordsToLatLng static method. 似乎意思是需要经纬度。。。。

遂改。。

 var result = serviceResult.result;
 L.geoJSON(result.recordsets[0].features, {
     coordsToLatLng: function (coords) {
                            return L.CRS.EPSG3857.unproject(L.point(coords[0], coords[1]));
                        }
}).addTo(map);

这个时候出来了。。。。

总结:1、几何查询对象的坐标系要与地图坐标系保持一致

2、geojson要经纬度,不是经纬度的要先转成经纬度。

转载自:https://blog.csdn.net/fengyekafei/article/details/79852965

You may also like...