我正在尝试让样式在从外部文件加载多边形的 Bing map 上工作。该文档有两个示例,第一个是直接在 javascript 中包含数据:
//Load the GeoJson Module.
Microsoft.Maps.loadModule('Microsoft.Maps.GeoJson', function () {
//Parse the GeoJson object into a Bing Maps shape.
var shape = Microsoft.Maps.GeoJson.read(myGeoJson, {
polygonOptions: {
fillColor: 'rgba(255,0,0,0.5)',
strokeColor: 'white',
strokeThickness: 5
}
});
//Add the shape to the map.
map.entities.push(shape);
});
(其中“myGeoJson”是先前定义的变量)
第二个从文件加载它:
//Load GeoJSON module.
Microsoft.Maps.loadModule('Microsoft.Maps.GeoJson', function () {
//Read the GeoJSON file that is hosted on the same domain.
Microsoft.Maps.GeoJson.readFromUrl('data/Countries.js',
function (shapes) {
//Add the shape(s) to the map.
map.entities.push(shapes);
});
});
我希望能够像第二个示例一样从文件加载,但要像第一个示例一样设置样式。
我不知道该怎么做。我试过简单地将第一个示例中对 GeoJson.read() 的调用替换为对 GeoJson.readFromUrl() 的调用,但这失败了。该文档没有提供有关如何设置从文件加载的 geojson 样式的任何线索。
谁能给我任何提示或提示?
最佳答案
尝试以下操作:
//Load GeoJSON module.
Microsoft.Maps.loadModule('Microsoft.Maps.GeoJson', function () {
//Read the GeoJSON file that is hosted on the same domain.
Microsoft.Maps.GeoJson.readFromUrl('data/Countries.js',
function (shapes) {
//Add the shape(s) to the map.
map.entities.push(shapes);
}, null, {
polygonOptions: {
fillColor: 'rgba(255,0,0,0.5)',
strokeColor: 'white',
strokeThickness: 5
}
});
注意回调函数和样式之间的“空”。如果需要使用 JSONP 请求您的文件,您可以在该字段中指定 JSONP URL 参数。
关于javascript - 从 Bing Maps API 中的 URL 添加 PolygonOptions 到 GeoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576183/