首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在react中使用打开层显示变频器层

无法在react中使用打开层显示变频器层
EN

Stack Overflow用户
提问于 2018-09-07 05:47:48
回答 1查看 1.2K关注 0票数 2

我正在努力在一个创建-反应-应用程序项目中添加一个带有本地geojson文件源的向量层,但是向量层无法显示。

这是我的代码:

代码语言:javascript
复制
import React,{ Component } from 'react'
import './BaseMap.scss'
import 'ol/ol.css'
import {Map, View} from 'ol'
import TileLayer from 'ol/layer/Tile'
import {fromLonLat} from 'ol/proj'
import XYZ from 'ol/source/XYZ'
import {defaults, ScaleLine } from 'ol/control'
import GeoJSON from 'ol/format/GeoJSON';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';

const vectorLayer = new VectorLayer({
    source: new VectorSource({
        url: './shanghang_lj.json',
        format: new GeoJSON()
    }),
    style: function(feature) {
        style.getText().setText(feature.get('name'));
        return style;
    }
 });


const tian_di_tu_road_layer = new TileLayer({
    title: "road_layer",
    source: new XYZ({
        url: "http://t4.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}"
    })
});
const tian_di_tu_annotation = new TileLayer({
    title: "annotation",
    source: new XYZ({
        url: 'http://t4.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}'
    })
});


export class BaseMap extends Component{
    constructor(props){
        super(props);

    };


    componentDidMount(){
        let map = new Map({
            controls: new defaults({
                attributionOptions: {
                    collapsible: false
                }
              }).extend([
                new ScaleLine()
              ]),
            target:'basemap',
            layers:[
                tian_di_tu_road_layer,//can be displayed
                tian_di_tu_annotation,//can be displayed
                vectorLayer//can not be displayed
            ],
            view:new View({
                center:fromLonLat([116.4,25.0]),
                zoom:10
            })
        })

    }


    render(){
        return(
           <div id="basemap" className='basemap'></div>    
        )
    }
}

我从控制台得到了错误:

代码语言:javascript
复制
Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at getObject (JSONFeature.js:188)
at GeoJSON.readFeatures (JSONFeature.js:56)
at VectorSource.<anonymous> (featureloader.js:87)

然后,我尝试了另一种传递geojson数据的方法:

代码语言:javascript
复制
import border_shanghang from './shanghang_lj.json';

const vectorLayer = new VectorLayer({
    source: new VectorSource({
        features: (new GeoJSON()).readFeatures(border_shanghang)
    }),
});

控制台中没有出现错误,但仍然没有显示向量层。

我和这个问题斗争很久了。任何帮助都将不胜感激。

顺便说一句,如果我在没有反应的情况下运行代码,代码就能工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-07 06:38:22

我尝试用一个样例geojson文件来实现这一点,对我起作用的是:

代码语言:javascript
复制
import border_shanghang from './shanghang_lj.geojson'; // I got a sample geojson from the web
const style = // ...;
const vectorLayer = new VectorLayer({
  source: new VectorSource({
    url: border_shanghang,
    format: new GeoJSON()
  }),
  style: function (feature) {
    style.getText().setText(feature.get('name'));
    return style;
  }
});
// ...

其他一切都一样。唯一的区别是将导入直接放到VectorSource的VectorSource字段中,并将文件从.json重命名为.geojson

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52216011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档