javascript - Leaflet没有用geojsonFeatures标记点?

标签 javascript json leaflet geojson

我开始使用 Leaflet API,希望使用 GeoJson 数据向客户 map 添加一些几何点。目前,我使用了此处提供的 geojsonFeature 类型 http://leafletjs.com/examples/quick-start.html

但是,当我按照这样的步骤添加功能时,这些功能不会出现。下面是我的 html 页面和代码。

<html><head>
    <title>Leaflet Quick Start Guide Example</title>
    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css">
</head>
<body class="">
    <div id="mapid" style="width: 600px; height: 400px; position: relative;" class="leaflet-container leaflet-fade-anim" tabindex="0"></div>

    <script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
    <script src="https://raw.github.com/calvinmetcalf/leaflet-ajax/master/dist/leaflet.ajax.min.js"></script>
    <script>

        var mymap = L.map('mapid').setView([51.505, -0.09], 13);

        L.tileLayer('https://api.mapbox.com/styles/v1/edsonbarboza/ciqb82pfe000dc1nliqy8sljp/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoiZWRzb25iYXJib3phIiwiYSI6ImNpbTJjMWRuczA5NGx1MGtzbmN6c3NjOHMifQ.FvAMXzmnQ0TIJDDsV6rXAw', {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
                '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
                'Imagery © <a href="http://mapbox.com">Mapbox</a>',
            id: 'mapbox.satelity'
        }).addTo(mymap);


        var myStyle = {
        "color": "#ff7800",
        "weight": 5,
        "opacity": 0.65
        };


        //L.marker([51.51, -0.09]).addTo(mymap)

        var geojsonFeature = {
            "type": "Feature",
            "properties": {
                "name": "Coors Field",
                "amenity": "Baseball Stadium",
                "popupContent": "This is where the Rockies play!"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [51.51, -0.09]
            }
        };

        var myLayer = L.geoJson().addTo(mymap);
        myLayer.addData(geojsonFeature);

        <!-- var geojsonLayer = new L.GeoJSON.AJAX("geojson_data.json", {style:myStyle}); -->

        <!-- geojsonLayer.addTo(mymap); -->

        var popup = L.popup();

        function onMapClick(e) {
            popup
                .setLatLng(e.latlng)
                .setContent("You clicked the map at " + e.latlng.toString())
                .openOn(mymap);
        }

        mymap.on('click', onMapClick);

    </script>

感谢您的帮助。

最佳答案

您必须反转几何中的坐标。

 var geojsonFeature = {
        "type": "Feature",
        "properties": {
            "name": "Coors Field",
            "amenity": "Baseball Stadium",
            "popupContent": "This is where the Rockies play!"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-0.09, 51.51]
        }
    };

这是您的sample有修正

这是指定的here

关于javascript - Leaflet没有用geojsonFeatures标记点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38248689/

相关文章:

json - 在golang中的json unmarshal上获取可为空的对象

shiny - 在不重新绘制传单 map 的情况下进行 Shiny 的 UI 调整

r - 使用 R 中的传单库绘制跨越国际日期变更线的路线

javascript - 定义函数时如何在javascript函数中初始化参数

javascript - 将输入的文本保存到 cookie 并在用户每次访问时显示给用户

javascript - 为什么 jQuery $(document).ready() 在验证失败后没有在 Rails 渲染 View 中触发?

javascript - 如何将Popup多个功能属性与传单绑定(bind)?

json - GraphQL 和 rest api 有什么区别

javascript - 在leaft.draw中,使用事件draw :deletestop,如何获取我想要删除的特定图层

javascript - 从javascript对象获取数据