我在javascript中有一个字符串变量作为statesData,当我在控制台中打印它时 console.log(statesData)它给我输出
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
74.987712893238,
16.4611124595779
],
[
75.0794873366632,
16.4216041790002
],
[
74.9914167945422,
16.5154363453721
],
[
74.987712893238,
16.4611124595779
]
]
]
},
"properties": {
"f1": "Bagalkot District",
"f2": 4,
"f3": 0.0327269593968744
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.6708769115599,
14.7838624856797
],
[
75.7046235678866,
14.7698699696418
],
[
75.7497789666211,
14.9338893073175
],
[
75.6988619436357,
14.9320185378459
],
[
75.721037483182,
14.8603111097135
],
[
75.6708769115599,
14.7838624856797
]
]
]
},
"properties": {
"f1": "Ballari District",
"f2": 7,
"f3": 0.0391301194948449
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
74.0400818803335,
15.6369930144092
],
[
74.0643009308787,
15.629971642517
],
[
74.0855287111498,
15.6477446840837
],
[
74.0655222616442,
15.6769214138993
],
[
74.0409889333752,
15.684796356228
],
[
74.0205207356315,
15.6756395243798
],
[
74.039428399187,
15.657777976182
],
[
74.0400818803335,
15.6369930144092
]
]
]
},
"properties": {
"f1": "Belagavi District",
"f2": 8,
"f3": 0.0281856872997076
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
77.2207332227667,
13.1629012169715
],
[
77.2411285265379,
13.1488604730127
],
[
77.2001264894172,
13.1832957390292
],
[
77.2083747838975,
13.179093776468
],
[
77.2207332227667,
13.1629012169715
]
]
]
},
"properties": {
"f1": "Bengaluru Urban District",
"f2": 86,
"f3": 0.833530387188893
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.1765764664482,
13.2837112032172
],
[
75.1542222218667,
13.2789357676158
],
[
75.1772089066226,
13.2170710348114
],
[
75.2039151953973,
13.3434386684308
],
[
75.2020812594624,
13.3100521461702
],
[
75.1765764664482,
13.2837112032172
]
]
]
},
"properties": {
"f1": "Chikkamagaluru District",
"f2": 2,
"f3": 0.0138095694326863
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
76.1116709003931,
13.9349464503589
],
[
76.1038291497053,
13.9046996994902
],
[
76.0847056753228,
14.056410903216
],
[
76.0801950024902,
13.9465147806955
],
[
76.1116709003931,
13.9349464503589
]
]
]
},
"properties": {
"f1": "Chitradurga District",
"f2": 3,
"f3": 0.0156783102632625
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
74.8653521851079,
13.0476877454595
],
[
74.7786075719437,
13.0493299278648
],
[
75.0699524730125,
13.0631902614285
],
[
74.9642609277546,
13.0529854233513
],
[
74.8653521851079,
13.0476877454595
]
]
]
},
"properties": {
"f1": "Dakshina Kannada District",
"f2": 18,
"f3": 0.197682627967718
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.5103745217132,
14.1179833401105
],
[
75.5729292992944,
14.0397898681339
],
[
75.6996850328144,
14.0570747408866
],
[
75.7737630588975,
14.1146909833957
],
[
75.6160317699427,
14.3742302168837
],
[
75.5605851345746,
14.2633369461476
],
[
75.5103745217132,
14.1179833401105
]
]
]
},
"properties": {
"f1": "Davangere District",
"f2": 7,
"f3": 0.0720749586059508
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
74.723340940517,
15.4748459289395
],
[
74.7288856040538,
15.4055376347295
],
[
74.8342342112531,
15.6106901855913
],
[
74.7981938982639,
15.4665289336343
],
[
74.723340940517,
15.4748459289395
]
]
]
},
"properties": {
"f1": "Dharwad District",
"f2": 29,
"f3": 0.294270499381753
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.2790864624982,
15.6291750722057
],
[
75.409134552733,
15.5806128106624
],
[
75.4033729284821,
15.4983038927923
],
[
75.4280656038431,
15.8407089911319
],
[
75.3251794565055,
15.8176624941283
],
[
75.2790864624982,
15.6291750722057
]
]
]
},
"properties": {
"f1": "Gadag District",
"f2": 3,
"f3": 0.031949373198826
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.6220536370659,
12.8189890581004
],
[
75.6220536370658,
12.8189890581003
],
[
75.6759493105302,
12.9140842549367
],
[
75.6519952502805,
12.9075942794764
],
[
75.6220536370659,
12.8189890581004
]
]
]
},
"properties": {
"f1": "Hassan Distric",
"f2": 2,
"f3": 0.0144830182711203
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.0227527715045,
14.7263163514708
],
[
75.0449314256517,
14.6570080572607
],
[
75.1863203458402,
14.6431463984187
],
[
75.3506952210452,
15.0102120098225
],
[
75.1946373411455,
14.9952325330059
],
[
75.0698824115673,
14.9148349117222
],
[
75.0227527715045,
14.7263163514708
]
]
]
},
"properties": {
"f1": "Haveri District",
"f2": 3,
"f3": 0.0372722864393961
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.2703710228573,
16.6020450061215
],
[
75.3249369342604,
16.5671446456902
],
[
75.4441472003017,
16.5006086832485
],
[
75.3554325837128,
16.7113058976472
],
[
75.2887655722583,
16.7147116212027
],
[
75.2703710228573,
16.6020450061215
]
]
]
},
"properties": {
"f1": "Vijayapura District",
"f2": 7,
"f3": 0.0326766444387371
}
}
]
}
我修改了这些数据并使用链接http://geojsonlint.com/进行了测试 在哪里创建Geojson对象层。但是当我在地图脚本中使用它时
geojson = L.geoJson(statesData, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
它给我一个错误,因为未捕获错误:无效的GeoJSON对象。
我没有得到错误的原因。我需要将我的字符串变量statesData解析为Geojson。如果是,那么我如何解析字符串数据到GEOJSON。
请帮帮我......
答案 0 :(得分:5)
您只需要将String转换为JSON:
statesData = JSON.parse(statesData)