我的GeoJSON的结构不允许LeafLet显示图层上的点

时间:2017-07-29 20:24:09

标签: java json leaflet geojson

我正在使用leaflet-ajax项目(https://github.com/calvinmetcalf/leaflet-ajax),我想声明一个这样的geojsonLayer变量:

var geojsonLayer = L.geoJson.ajax("http://localhost:7070/findInArea");

其中http://localhost:7070/findInArea是我编码的Dropwizard REST Web服务的端点。

在尝试使用http://localhost:7070/findInArea作为参数之前,我尝试将包含我的REST Web服务返回的JSON文件作为参数:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "imo": 0,
            "course": "0.0",
            "description": "description",
            "mmsi": 432473000,
            "type": "VESSEL",
            "heading": "NaN"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-4.064533333333333, 5.29355]
        }
    }, {
        "type": "Feature",
        "properties": {
            "imo": 0,
            "course": "0.0",
            "description": "description",
            "mmsi": 375488000,
            "type": "VESSEL",
            "heading": "NaN"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-4.025521666666666, 5.303138333333333]
        }
    }, {
        "type": "Feature",
        "properties": {
            "imo": 0,
            "course": "0.0",
            "description": "description",
            "mmsi": 355794000,
            "type": "VESSEL",
            "heading": "NaN"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-4.01319, 5.28968]
        }
    }]
}

当我尝试使用此JSON时,点不会显示在图层上。我在网上搜索了一个(Geo)JSON的例子。在https://github.com/calvinmetcalf/leaflet-ajax/tree/gh-pages/example我发现了一个:

{
   "type":"FeatureCollection",
   "features":[
      {
         "geometry":{
            "type":"Point",
            "coordinates":[
               -71.123723098996251,
               42.379003083976961
            ]
         },
         "type":"Feature",
         "id":0,
         "properties":{
            "UNDERGRAD":43,
            "CITYST":"Cambridge, MA",
            "OBJECTID":72.0,
            "URL":"http://www.longy.edu",
            "TYPE":"PRI",
            "DESCRIPTN":"4-year or above",
            "NCES_ID":"166489",
            "ZIPCODE":"02138",
            "L_ACC_EST":16,
            "SITE":"1",
            "L_SRC_1":"nces.ed.gov",
            "L_BASE":"DOQ",
            "DEGREES":"C, M",
            "L_TYPE":"CB",
            "ADDRESS":"One Follen St",
            "COLLEGE":"Longy School Of Music",
            "L_METH":"IN-WEBSITE",
            "GRAD":137,
            "MAIN_TEL":"(617) 876-0956"
         }
      },
      {
         "geometry":{
            "type":"Point",
            "coordinates":[
               -71.308156671517793,
               42.643612284195882
            ]
         },
         "type":"Feature",
         "id":1,
         "properties":{
            "UNDERGRAD":79,
            "CITYST":"Lowell, MA",
            "OBJECTID":73.0,
            "URL":"http://www.lowellacademy.com",
            "TYPE":"PRI",
            "DESCRIPTN":"less-than-2-year",
            "NCES_ID":"166498",
            "ZIPCODE":"01852",
            "L_ACC_EST":16,
            "SITE":"1",
            "L_SRC_1":"nces.ed.gov",
            "L_BASE":"DOQ",
            "DEGREES":"C",
            "L_TYPE":"CB",
            "ADDRESS":"136 Central St",
            "COLLEGE":"Lowell Academy Of Hairdressing",
            "L_METH":"IN-VERB",
            "GRAD":0,
            "MAIN_TEL":"(978) 453-3235"
         }
      }
   ]
}

目前我的REST Web服务的Java代码是:

@Override
public FeatureCollection findTracksInACertainArea(double longitudeMin, double longitudeMax, double latitudeMin, double latitudeMax){

    FeatureCollection fc = new FeatureCollection();
    List<Feature> features = new ArrayList<Feature>();
    DBCursor cursor = null;

    BasicDBObject query = new BasicDBObject();
    query.put("detection.position.0", BasicDBObjectBuilder.start("$gte", longitudeMin).add("$lte", longitudeMax).get());
    query.put("detection.position.1", BasicDBObjectBuilder.start("$gte", latitudeMin).add("$lte", latitudeMax).get());

    cursor = collection.find(query);

    while(cursor.hasNext()) {
        final Track track = TrackDAOHelper.convertTrackFromDBObject(cursor.next());
        features.add(buildFeature(track));
    }

    fc.setFeatures(features);
    return fc;
}

private Feature buildFeature(Track track) {
    Feature feature = new Feature();
    Point point = new Point(track.getDetection().getPosition().getLongitude(), track.getDetection().getPosition().getLatitude());
    feature.setGeometry(point);
    Map<String, Object> properties = buildProperties(track);
    feature.setProperties(properties);
    return feature;
}

private Map<String, Object> buildProperties(Track track) {
    Map<String, Object> properties = new HashMap<String, Object>();

    if (track != null) {
        if (track.getName() != null)
            properties.put("name", track.getName());

        properties.put("course", "0.0");
        properties.put("heading", "NaN");
        properties.put("type", "VESSEL");

        if (track.getMmsi() != null)
            properties.put("mmsi", track.getMmsi());

        if (track.getImo() != null)
            properties.put("imo", track.getImo());

        if (track.getCallsign() != null)
            properties.put("callsign", track.getCallsign());

        String description = buildDescription();
        properties.put("description", description);
    }
    return properties;
}

private String buildDescription() {
    return "description";
}

如何修改它以获得工作(Geo)JSON?

1 个答案:

答案 0 :(得分:0)

这不是由于JSON,而是由于Javascript代码