如何将JSON转换为GeoJson以与Google Maps API v3一起使用



我正在尝试使用Google Maps API在地图上创建一个包含33,000个点的集群。首先,请允许我提供一些背景资料。

背景信息:

在谷歌地图API文档中,有一个名为loadGeoJson()的函数。

loadGeoJson()的回调将从 XHR 请求返回的 JSON 对象转换为 API 可用的对象。该对象如下所示:

(2) [_.Ke, _.Ke]  
0: _.Ke {m: undefined, j: _.Ze, l: {…}, closure_uid_258027441: 237, __e3_: {…}}  
1: _.Ke {m: undefined, j: _.Ze, l: {…}, closure_uid_258027441: 238, __e3_: {…}}  
length: 2
__proto__: Array(0)

此对象旨在与 API 一起使用。我可以使用这种表示法调用features.map()feature.getGeometry().get(0)等方法。

<小时 />

问题:

如何采用格式化为 GeoJson 的 JSON 对象,如下所示:

{features: Array(33156), type: "FeatureCollection"}
features: Array(33156)
[0 … 9999]
[10000 … 19999]
[20000 … 29999]
[30000 … 33155]
length: 33156
__proto__: Array(0)
type: "FeatureCollection"
__proto__: Object

并将其转换为 Google Maps GeoJson 对象,无需使用 AJAX,即可由 API 使用?

我的对象就在我的代码中,但它无法使用,因为我没有从 AJAX 请求中获取对象?这说不通。

注:使用addGeoJson()可显示地图上的所有 33,000+ 要素(点),而没有机会使用该数据创建聚类。太慢了!加载需要 ~18 秒(使用秒表计时)。

我找不到将 JSON 转换为 API "可用"的 GeoJson 对象的方法,所以我只使用了 JSON 对象本身并将功能"映射"到标记中。

// Map all Features to Markers
var markers = geoJson.features.map(function (feature) {
return new google.maps.Marker({
position: { lat: feature.geometry.coordinates[1], lng: feature.geometry.coordinates[0] }
});
});
// Make The Clusterizer
var clusterizer = new MarkerClusterer(_map, markers,
{ imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' });
console.log(geoJson);

这比在地图上渲染 33,000 个标记要快得多

(2秒与18秒相比)

最新更新