Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OpenLayers 3没有';t显示KML层_Java_Html_Kml_Openlayers 3 - Fatal编程技术网

Java OpenLayers 3没有';t显示KML层

Java OpenLayers 3没有';t显示KML层,java,html,kml,openlayers-3,Java,Html,Kml,Openlayers 3,我是OpenLayers的新手,我正在尝试向地图添加一个KML层 这是我的剧本: var projection = ol.proj.get('EPSG:3857'); var mapa = new ol.layer.Tile({ source: new ol.source.OSM() }); var map = new ol.Map({ target: 'map', controls: ol.control.defaults({ attribution

我是OpenLayers的新手,我正在尝试向地图添加一个KML层

这是我的剧本:

var projection = ol.proj.get('EPSG:3857');

var mapa = new ol.layer.Tile({
    source: new ol.source.OSM()
});

var map = new ol.Map({
    target: 'map',
    controls: ol.control.defaults({
        attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
        })
    }),
    view: new ol.View({
        center: [-5462834.47, -3058929.70],
        projection: projection,
        zoom: 10
    })
});

map.addLayer(mapa);

var alfalfa_zaragoza = new ol.layer.Vector({
    source: new ol.source.Vector({
        url: 'kml/ms_alfalfazaragoza_rgba_ndvi.kml',
        format: new ol.format.KML()
    }),
    extractStyles: true,
    extractAttributes: true,
    maxDepth: 2,
    setVisibility: true
});



document.getElementById('addKML').onclick = function () {
    map.addLayer(alfalfa_zaragoza);
};
和我的html页面:

<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="http://openlayers.org/en/v3.2.0/css/ol.css" type="text/css">
    <script type="javascript" src="<%=request.getContextPath()%>/resources/OpenLayersv3.14.2/build/ol.js"></script>
    <script src="http://maps.google.com/maps/api/js?v=3&amp;sensor=false"></script>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/OpenLayersv3.14.2/css/ol.css" type="text/css" />
    <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/ol3-google-maps-v0.4/ol3gm.css" type="text/css" />
    <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/layout.css" type="text/css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />

</head>
<body>

<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div id="map" class="map"></div>
        </div>
    </div>
    <div class="row-fluid">
        <div class="span12">
            <h4>Concept example</h4>

            <div class="row">
                <div class="col-md-4 col-sm-4">
                    <fieldset>
                        <input id="addKML" type="button" value="Add KML"
                               title="Add KML layer"/>
                    </fieldset>
                </div>
            </div>

        </div>
    </div>
</div>   
<script src="<%=request.getContextPath()%>/resources/ol3-google-maps-v0.4/ol3gm.js"></script>
<script src="<%=request.getContextPath()%>/resources/kml.js"></script>

</body>
</html>

标题
概念示例
错误在哪里?我们的想法是按下“addKML”按钮,图层被添加到地图中,但当我按下它时,什么都没有发生。

您只是忘记了这一行中的“新建”:

var projection = ol.proj.get('EPSG:3857');
替换为以下正确选项:

var projection = new ol.proj.get('EPSG:3857');