简体   繁体   English

显示“未定义”的弹出窗口的 mapbox geojson 描述

[英]mapbox geojson description for popup showing 'undefined'

I have the following code but when I click on a point to open a popup it returns 'undefined' and I cannot seem to work out why.我有以下代码,但是当我点击一个点打开一个弹出窗口时,它返回“未定义”,我似乎无法弄清楚原因。

I'm pulling the description field from the geoJSON external source which I have control over but for some reason it just does not want to populate the description HTML in my array.我正在从我可以控制的 geoJSON 外部源中提取描述字段,但由于某种原因,它只是不想在我的数组中填充描述 HTML。 I took the example for the popup from the mapbox website so I know it works there.我从 mapbox 网站上拿了弹出窗口的例子,所以我知道它在那里工作。 I have checked, rechecked and triple checked but I think I cannot see the tree for the forest lol.我已经检查,重新检查和三重检查,但我想我看不到森林的树哈哈。

Could someone maybe help me please?有人可以帮我吗? thanks.谢谢。

        <script>
        // ajax loading gif
        $(document).ready(function () {
            setTimeout(function () {
                $("#ajax-loader").removeClass("is-active");
            }, 6000);
        });

        // vars
        var initialOpacity = 0.2;
        var opacity = initialOpacity;

        // mapbox api
        mapboxgl.accessToken = "hidden_api_key";
        var map = new mapboxgl.Map({
            container: "map", // container ID
            style: "mapbox://styles/mapbox/dark-v10",
            center: [-2.582861, 53.5154517],
            zoom: 5,
            maxZoom: 16,
            minZoom: 0,
        });

        map.on("load", function () {
            // get hotspot locations
            map.addSource("hotspot_locations", {
                type: "geojson",
                data: "https://netmaker.io/dashboard/public_actions.php?a=ajax_helium_miners_location",
                cluster: false,
                clusterMaxZoom: 10, // max zoom to cluster points on
                clusterRadius: 50, // radius of each cluster when clustering points (defaults to 50)
            });

            // add 300m circle around each hotspot
            map.addLayer({
                id: "circle500",
                type: "circle",
                source: "hotspot_locations",
                paint: {
                    "circle-radius": {
                        stops: [
                            [0, 1],
                            [16, 600],
                        ],
                        base: 2,
                    },
                    "circle-color": "green",
                    "circle-opacity": 0.1,
                    "circle-stroke-width": 0,
                    "circle-stroke-color": "white",
                },
            });

            // add hotspot location
            map.addLayer({
                id: "hotspots-layer",
                type: "circle",
                source: "hotspot_locations",
                paint: {
                    "circle-radius": 2,
                    "circle-stroke-width": 2,
                    // "circle-color": "#36d293",
                    "circle-color": "white",
                    "circle-stroke-color": [
                        "match",
                        ["get", "status"],
                        "online",
                        "#36d293",
                        "offline",
                        "#d23636",
                        "orange", // other
                    ],
                    // "circle-stroke-color": '#36d293',
                },
            });
        });

        // ajax call hotspots location by name
        var customData;
        $.ajax({
            async: false,
            type: "GET",
            global: false,
            dataType: "json",
            url: "https://netmaker.io/dashboard/public_actions.php?a=ajax_helium_miners_location_customdata",
            success: function (data) {
                customData = data;
            },
        });

        // custom data using hotspot name
        function forwardGeocoder(query) {
            var matchingFeatures = [];
            for (var i = 0; i < customData.features.length; i++) {
                var feature = customData.features[i];
                if (feature.properties.title.toLowerCase().search(query.toLowerCase()) !== -1) {
                    // feature["place_name"] = '<img src="https://netmaker.io/dashboard/images/helium_logo.svg" alt="" width="15px">  ' + feature.properties.title;
                    feature["place_name"] = feature.properties.title;
                    feature["center"] = feature.geometry.coordinates;
                    feature["place_type"] = ["hotspot"];
                    matchingFeatures.push(feature);
                }
            }
            return matchingFeatures;
        }

        // add the control to the map.
        map.addControl(
            new MapboxGeocoder({
                accessToken: mapboxgl.accessToken,
                localGeocoder: forwardGeocoder,
                zoom: 14,
                placeholder: "Search: address or hotspot name",
                mapboxgl: mapboxgl,
            })
        );

        map.on("click", "hotspots-layer", function (e) {
            var coordinates = e.features[0].geometry.coordinates.slice();
            var description = e.features[0].properties.description;

            while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
                coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
            }

            new mapboxgl.Popup().setLngLat(coordinates).setHTML(description).addTo(map);
        });

        map.on("mouseenter", "hotspots-layer", function () {
            map.getCanvas().style.cursor = "pointer";
        });

        map.on("mouseleave", "hotspots-layer", function () {
            map.getCanvas().style.cursor = "";
        });
    </script>

You're recieving an undefined because e.features[0].properties.description doesn't exist in the "hotspots-layer" data.您收到的是undefined因为e.features[0].properties.description在“热点层”数据中不存在。

 "features": [ { "type": "Feature", "properties": { "status": "online" }, "geometry": { "type": "Point", "coordinates": [ "119.73479929772", "30.240836896893", 0 ] } },

The only "description" in this case that you can return is the e.features[0].properties.status as seen here:在这种情况下,您可以返回的唯一“描述”是e.features[0].properties.status如下所示:

 map.on("click", "hotspots-layer", function (e) { var coordinates = e.features[0].geometry.coordinates.slice(); var description = e.features[0].properties.status; while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) { coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360; } console.log(description) new mapboxgl.Popup().setLngLat(coordinates).setHTML(description).addTo(map); });

This code snippet will allow the click event to show you the status when you interact with the hotspot location.当您与热点位置交互时,此代码段将允许点击事件向您显示status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM