javascript - 使用正方形而不是点

标签 javascript leaflet

我使用leafletjs在 map 上显示一些点。数据源是geoJSON

喜欢this他们网站上提供的简单示例。

enter image description here

是否可以通过样式使用方形而不是点,或者唯一的方法是使用multiPolygon而不是multiPoint

最佳答案

我希望这有帮助:

HTML

<div id="map" style="width: 600px; height: 400px"></div>

JavaScript

var map = L.map('map').setView([40, -100], 15),
    createSquare = function (latlng, options) {
        var point = map.latLngToContainerPoint(latlng),
            size = options.radius || options.size || 10,
            point1 = L.point(point.x - size, point.y - size),
            point2 = L.point(point.x + size, point.y + size),
            latlng1 = map.containerPointToLatLng(point1),
            latlng2 = map.containerPointToLatLng(point2);
        return new L.rectangle([latlng1, latlng2], options);
    },
    points1 = {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [-100, 40]
                }
            },
            {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [-99.999, 40]
                }
            }
        ]
    },
    points2 = {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [-100, 39.999]
                }
            },
            {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [-99.999, 39.999]
                }
            }
        ]
    },    
    layer1 = L.geoJson(points1, {
        pointToLayer: function (feature, latlng) {
            return new L.CircleMarker(latlng, {
                radius: 10,
                color: "#000",
                weight: 1,
                fillColor: "#F50",
                fillOpacity: 0.75
            });
        }
    }).addTo(map),
    layer2 = L.geoJson(points2, {
        pointToLayer: function (feature, latlng) {
            return createSquare(latlng, {
                radius: 10,
                color: "#000",
                weight: 1,
                fillColor: "#0F5",
                fillOpacity: 0.75
            });
        }
    }).addTo(map);

结果

enter image description here

Here是jsfiddle。

关于javascript - 使用正方形而不是点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742642/

相关文章:

javascript - 如何将 “readonly” 属性添加到 &lt;input&gt;?

javascript - 如何从 jQuery.find() 结果创建 XMLDocument

javascript - 传单 js 中心弹出问题(仅在少数国家/地区)

r - 使用 Shiny R 将 react 性弹出图/图添加到 Leaflet map

javascript - 如何使用 css 对齐传单 map 中的响应图像

javascript - Gmail、nodemailer、OATH2 刷新 token 不起作用

javascript - 表列大小调整为 100% 容器高度

javascript - 如何在 Leaflet map 上正确绘制多边形(纬度/经度顶点和旋转)?

javascript - 如何使用javascript删除html标签之间的空白?

angular - 如何使用 Angular 服务提供 leaflet 或 google maps 等第三方 API?