简体   繁体   English

从Bing Maps API中的URL向GeoJSON添加PolygonOptions

[英]Add PolygonOptions to GeoJSON from URL in Bing Maps API

I'm trying to get styles working on a Bing map that loads polygons from an external file. 我正在尝试使用Bing地图上的样式来加载外部文件中的多边形。 The documentation has two examples, firstly for including the data directly in the javascript: 该文档有两个例子,首先是直接在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);
    });

(where 'myGeoJson' is a previously defined variable) (其中'myGeoJson'是先前定义的变量)

and the second loads it from a file: 第二个从文件中加载它:

    //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);
            });
    });

I want to be able to load from file, as in the second example, but style it as in the first. 我希望能够从文件加载,如第二个示例中所示,但在第一个示例中将其设置为样式。

I'm having trouble working out how to do that. 我在弄清楚如何做到这一点时遇到了麻烦。 I've tried simply replacing the call to GeoJson.read() in the first example with a call to GeoJson.readFromUrl(), but this fails. 我试过在第一个例子中通过调用GeoJson.readFromUrl()来简单地替换对GeoJson.read()的调用,但是这个失败了。 The documentation doesn't give any clues about how to style geojson loaded from file. 该文档没有提供任何关于如何设置从文件加载的geojson样式的线索。

Can anyone give me any hints or tips? 任何人都可以给我任何提示或提示吗?

Try the following: 请尝试以下方法:

//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
            }
        });

Note the "null" between the callback function and the styles. 注意回调函数和样式之间的“null”。 If your file needed to be requested using JSONP, you can specify the JSONP URL parameter in that field. 如果需要使用JSONP请求文件,则可以在该字段中指定JSONP URL参数。

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

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