![](/img/trans.png)
[英]How can I access dataobjects in geoJSON data, placed on Google Maps
[英]How i can access geometry inside GeoJson
在这里再次学习深度 openlayers-6,现在我想从作为图层加载的 GeoJson 文件访问几何图形,我该怎么做?提前感谢您的帮助。
import 'ol/ol.css';
import Feature from 'ol/Feature';
import Geolocation from 'ol/Geolocation';
import Map from 'ol/Map';
import View from 'ol/View';
import Point from 'ol/geom/Point';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import OSM from 'ol/source/OSM';
import VectorSource from 'ol/source/Vector';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
import GeoJSON from 'ol/format/GeoJSON';
var view = new View({
center: [0, 0],
zoom: 2
});
var openstreet = new TileLayer({
source: new OSM()
});
var geozonas = new VectorLayer({
source: new VectorSource({
url:'https://geo.anantara.cl/maps/json/geozone2.json',
format: new GeoJSON()
})
});
var map = new Map({
layers: [
openstreet, geozonas
],
target: 'map',
view: view
});
继续调查,我发现了一个名为 featureloader.js 的文件,其中调用了读取 geojson 文件的 function,但我找不到它的存储位置,显然他没有保持完整,但显然执行了一种计算。 你还有其他关于它的信息吗?
export function loadFeaturesXhr(url, format, success, failure) {
return (
/**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection.
* @this {import("./source/Vector").default|import("./VectorTile.js").default}
*/
function (extent, resolution, projection) {
var xhr = new XMLHttpRequest();
console.log(new Error().stack); //psilva
xhr.open('GET', typeof url === 'function' ? url(extent, resolution, projection) : url, true);
if (format.getType() == FormatType.ARRAY_BUFFER) {
xhr.responseType = 'arraybuffer';
}
xhr.withCredentials = withCredentials;
/**
* @param {Event} event Event.
* @private
*/
xhr.onload = function (event) {
// status will be 0 for file:// urls
if (!xhr.status || xhr.status >= 200 && xhr.status < 300) {
var type = format.getType();
/** @type {Document|Node|Object|string|undefined} */
var source = void 0;
if (type == FormatType.JSON || type == FormatType.TEXT) {
source = xhr.responseText;
}
else if (type == FormatType.XML) {
source = xhr.responseXML;
if (!source) {
source = new DOMParser().parseFromString(xhr.responseText, 'application/xml');
}
}
else if (type == FormatType.ARRAY_BUFFER) {
source = /** @type {ArrayBuffer} */ (xhr.response);
}
if (source) {
success.call(this, format.readFeatures(source, {
extent: extent,
featureProjection: projection
}), format.readProjection(source));
}
else {
failure.call(this);
}
}
else {
failure.call(this);
}
}.bind(this);
/**
* @private
*/
xhr.onerror = function () {
failure.call(this);
}.bind(this);
xhr.send();
});
}
在调试了 openlayers 示例https://openlayers.org/en/latest/examples/select-features.html 之后,很清楚且令人信服的是,geojson 被加载到了一个名为 ZA8CFDE6331BD59EB2ACZ6B96 的特性和特性中,然后它允许你查看特性,属性如图所示。
这些坐标是根据到平面的投影进行数学变换后存储的,因此,回到我最初的问题,答案是访问这个 object 并且有坐标但不是原始坐标,而是已经处理过的坐标在加载过程中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.