简体   繁体   English

在Mapboxgl.js和GeoJSON中使用“圆形颜色”数据驱动的样式时出现错误,但使用默认颜色可以正常工作

[英]Getting error while using 'circle-color' data driven styling in Mapboxgl.js & GeoJSON, but working fine with default color

I am trying to create a map, put data points using GeoJSON and Mapbox. 我正在尝试创建地图,使用GeoJSON和Mapbox放置数据点。 It works fine when I am using a default color code for all points, but when I try to use data driven styling to put different colors for different property values It is giving errors. 当我为所有点使用默认颜色代码时,它工作正常,但是当我尝试使用数据驱动的样式为不同的属性值放置不同的颜色时,它给出了错误。 I am using mapboxgl.js. 我正在使用mapboxgl.js。

I get the following errors in Chrome Inspect: 我在Chrome Inspect中收到以下错误:

net::ERR_INTERNET_DISCONNECTED
exports.getJSON @ ajax.js:33
evented.js:111 Error
at XMLHttpRequest.r.onerror (ajax.js:18)

Please help! 请帮忙! Here are my GeoJSON and HTML files. 这是我的GeoJSON和HTML文件。

mapboxgl.accessToken = 'pk.eyJ1Ijoicml0YW1iaGFyYSIsImEiOiJjajZuNGZjNHUwNHgxMzNwc29hZ2ZkbmRvIn0.4kTuXEpbJBeoN3jCp3pfwQ';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/dark-v9',
    center: [-121.403732, 40.492392],
    zoom: 10
});
map.on("load", function() {
    map.addSource('pH', {
        'type': 'geojson',
         'data': 'test.json'
    });
    map.addLayer({
        id: 'heat-map',
        type: 'circle',
        source: 'pH',
        paint: {
            // 'circle-color': '#f1f075',
            'circle-color': {
                property: 'value',
                 stops: [
                    [6, '#f1f075'],
                    [10, '#e55e5e']
                ]          
            },
            "circle-radius": 6,
            'circle-opacity': 0.8
        },
    });
});

GeoJSON file: GeoJSON文件:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": { "value": "7" }, 
        "geometry": {
            "type": "Point",
            "coordinates": [-121.415061, 40.506229]
        }
    }, {
        "type": "Feature",
        "properties": { "value": "8" }, 
        "geometry": {
            "type": "Point",
            "coordinates": [-121.505184, 40.488084]
        }
    }, {
        "type": "Feature",
        "properties": { "value": "9" }, 
        "geometry": {
            "type": "Point",
            "coordinates": [-121.354465, 40.488737]
        }
    }]
}

I believe the problem is due to the fact that your value s are in the geojson as strings not numbers. 我认为问题是由于您的value s在geojson中是字符串而不是数字。 When I changed them to numbers as below your example code worked for me 当我将它们更改为数字时,如下所示,您的示例代码对我有用

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": { "value": 7 }, 
        "geometry": {
            "type": "Point",
            "coordinates": [-121.415061, 40.506229]
        }
    }, {
        "type": "Feature",
        "properties": { "value": 8 }, 
        "geometry": {
            "type": "Point",
            "coordinates": [-121.505184, 40.488084]
        }
    }, {
        "type": "Feature",
        "properties": { "value": 9 }, 
        "geometry": {
            "type": "Point",
            "coordinates": [-121.354465, 40.488737]
        }
    }]
}

If you still get the AJAX error you may need to run a local server ( you can do this with python -m SimpleHTTPServer in your project folder and then load localhost:8000/path/to/index.html in your browser) 如果仍然出现AJAX错误,则可能需要运行本地服务器(可以在项目文件夹中使用python -m SimpleHTTPServer进行此操作,然后在浏览器中加载localhost:8000/path/to/index.html

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

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