R rMaps geoJson 函数不绘图

R rMaps geoJson function not plotting

我有一条来自 ESRI 服务的 JSON 路线。我尝试使用 rMaps 使用 geoJson 绘制路线。我得到的唯一输出是地图,而不是多边形图。

这是代码:

require(rMaps)
json = "{\"messages\":[],\"routes\":{\"fieldAliases\":{\"ObjectID\":\"ObjectID\",\"Name\":\"Name\",\"FirstStopID\":\"FirstStopID\",\"LastStopID\":\"LastStopID\",\"StopCount\":\"StopCount\",\"Total_TravelTime\":\"Total_TravelTime\",\"Total_Kilometers\":\"Total_Kilometers\",\"Total_Miles\":\"Total_Miles\",\"Shape_Length\":\"Shape_Length\"},\"geometryType\":\"esriGeometryPolyline\",\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326},\"features\":[{\"attributes\":{\"ObjectID\":1,\"Name\":\"Location 1 - Location 2\",\"FirstStopID\":1,\"LastStopID\":2,\"StopCount\":2,\"Total_TravelTime\":2.8403004080455228,\"Total_Kilometers\":0.79761507718052971,\"Total_Miles\":0.49561503145413888,\"Shape_Length\":0.0082585488982182334},\"geometry\":{\"paths\":[[[-122.40774845199996,37.783745569000075],[-122.40745999999996,37.783510000000035],[-122.40722999999997,37.783340000000067],[-122.40718999999996,37.783310000000029],[-122.40648999999996,37.782740000000047],[-122.40591999999998,37.782300000000077],[-122.40520999999995,37.782870000000059],[-122.40442999999999,37.78350000000006],[-122.40432999999996,37.783580000000029],[-122.40372999999994,37.784070000000042],[-122.40329999999994,37.78374000000008],[-122.40269999999998,37.783270000000073],[-122.40411865599998,37.782150343000069]]]}}]},\"directions\":[{\"routeId\":1,\"routeName\":\"Location 1 - Location 2\",\"summary\":{\"totalLength\":0.49561503145413888,\"totalTime\":2.8403004049323499,\"totalDriveTime\":2.8403004080455228,\"envelope\":{\"xmin\":-122.40789999999998,\"ymin\":37.782000000000039,\"xmax\":-122.40269999999998,\"ymax\":37.784070000000042,\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}},\"features\":[{\"attributes\":{\"length\":0,\"time\":0,\"text\":\"Start at Location 1\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTDepart\"},\"compressedGeometry\":\"+1m91-6fki2+202vh+0+0\"},{\"attributes\":{\"length\":0.13919863031093585,\"time\":0.8808930174425843,\"text\":\"Go southeast on 5th St toward Stevenson St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTStraight\"},\"compressedGeometry\":\"+1m91-6fki2+202vh+36-2g\"},{\"attributes\":{\"length\":0.16963433548079213,\"time\":0.96497624999999998,\"text\":\"Turn left on Minna St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnLeft\"},\"compressedGeometry\":\"+1m91-6fkes+202t1+3q+33\"},{\"attributes\":{\"length\":0.07799196861297697,\"time\":0.51913014739269459,\"text\":\"Turn right on 4th St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnRight\"},\"compressedGeometry\":\"+1m91-6fkb2+20304+1p-1d\"},{\"attributes\":{\"length\":0.10879009704943393,\"time\":0.47530099321024388,\"text\":\"Turn right on Howard St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnRight\"},\"compressedGeometry\":\"+1m91-6fk99+202un-2f-1u\"},{\"attributes\":{\"length\":0,\"time\":0,\"text\":\"Finish at Location 2, on the left\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTStop\"},\"compressedGeometry\":\"+1m91-6fkbo+202sp+0+0\"}]}]}"
regions=RJSONIO::fromJSON(json)
lmap <- Leaflet$new()
lmap$tileLayer(provide='Stamen.TonerLite')
lmap$setView(c(37.78356, -122.4079), 13)
lmap$geoJson(
  regions)
legend_vec = c('red'='high', 'blue'='medium', 'green'='low')
lmap$legend(position = 'bottomright', 
            colors   =  names(legend_vec), 
            labels   =  as.vector(legend_vec))
lmap

有人知道为什么这个 JSON 没有显示在地图上吗?

谢谢!

这不是有效的 GeoJSON 对象。 L.GeoJSON(Leaflet 的 GeoJSON 层)只接受有效的 GeoJSON 特征集合对象,或具有有效 GeoJSON 特征对象的数组。这是一个有效的 GeoJSON 特征集合对象:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "myProperty": "myValue"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [0,0]
        }
    },{
        "type": "Feature",
        "properties": {
            "myProperty": "myValue"
        },
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [-45, -45],
                [45, 45]
            ]
        }
    }]
}

您可以在此处了解有关 GeoJSON 的更多信息:http://geojson.org/ and you can validate GeoJSON objects here: http://geojsonlint.com/ For good measure here is also the reference for Leaflet's L.GeoJSON layer: http://leafletjs.com/reference.html#geojson and a tutorial from Leaflet on how to use L.GeoJSON layers: http://leafletjs.com/examples/geojson.html