-1

I am getting a featurecollection returned from mapbox. The JSON looks like the following:

{"type":"FeatureCollection","query":["17","green"],"features":[{"id":"address.8882296350520732","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"rooftop"},"text":"Green Street","place_name":"17 Green Street, Brookline, Massachusetts 02446, United States","center":[-71.121411,42.343136],"geometry":{"type":"Point","coordinates":[-71.121411,42.343136]},"address":"17","context":[{"id":"postcode.8198763973790210","text":"02446"},{"id":"place.7864891969924050","wikidata":"Q49142","text":"Brookline"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.2730621404747834","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"rooftop"},"text":"Greenough Circle","place_name":"17 Greenough Circle, Brookline, Massachusetts 02445, United States","center":[-71.125244,42.334561],"geometry":{"type":"Point","coordinates":[-71.125244,42.334561]},"address":"17","context":[{"id":"postcode.7679295126168220","text":"02445"},{"id":"place.7864891969924050","wikidata":"Q49142","text":"Brookline"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.4738306377365208","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"interpolated"},"text":"Greenleaf Street","place_name":"17 Greenleaf Street, Boston, Massachusetts 02115, United States","center":[-71.090477,42.338387],"geometry":{"type":"Point","coordinates":[-71.090477,42.338387],"interpolated":true},"address":"17","context":[{"id":"neighborhood.295553","text":"Fenway"},{"id":"postcode.11169253348388930","text":"02115"},{"id":"place.9391334652012190","wikidata":"Q100","text":"Boston"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.2688910343185888","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"point"},"text":"Greenwich Street","place_name":"17 Greenwich Street, Roxbury Crossing, Massachusetts 02120, United States","center":[-71.083751,42.335527],"geometry":{"type":"Point","coordinates":[-71.083751,42.335527]},"address":"17","context":[{"id":"neighborhood.295329","text":"South End"},{"id":"postcode.7229336676579040","text":"02120"},{"id":"place.2024861849164830","wikidata":"Q20138","text":"Roxbury Crossing"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.4057524105398816","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"rooftop"},"text":"Greenwich Park","place_name":"17 Greenwich Park, Boston, Massachusetts 02118, United States","center":[-71.080331,42.342747],"geometry":{"type":"Point","coordinates":[-71.080331,42.342747]},"address":"17","context":[{"id":"neighborhood.294804","text":"Back Bay"},{"id":"postcode.8640649655199430","text":"02118"},{"id":"place.9391334652012190","wikidata":"Q100","text":"Boston"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]}],"attribution":"NOTICE: © 2019 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."}

The JSON output contains a FeatureCollection. Within the feature array, each feature has a property "place_name" which gives the full address of whatever place is listed. This is what I want to access.

Whenever I read this JSON into my android app and try to convert it to a FeatureCollection, I find that the "place_name" property is not part of a com.mapbox.geojson.Feature. I am using the following to convert the JSON to a FeatureCollection:

FeatureCollection.fromJson(response);

Where response is the returned JSON shown above. My question is: How do I access that property if MapBox does not include it as part of their com.mapbox.geojson.Feature? Is there some other class I should be using that is included by Mapbox?

user3479586
  • 159
  • 1
  • 12

1 Answers1

1

You could add that property to a feature before creating the collection:

Feature feature = new Feature().fromJson(response);

feature.addStringProperty("place_name", "17 Green Street, Brookline, Massachusetts 02446, United States");

FeatureCollection.fromFeature(feature);

Then, for querying:

feature.getProperties() to get all the properties associated with that feature, or feature.getStringProperty("place_name") to get a specific one.

Take a look at the Features api.

philoez98
  • 493
  • 4
  • 13
  • But this involves picking apart the JSON to access a specific property. It seems like a similar functionality should already exist somewhere since properties are a big part of a feature. The best I can come up with related to this is writing a custom json parser to love down to the feature level and add each property with addStringProperty. – user3479586 Jun 13 '19 at 12:19
  • It is indeed similar to what I'm actually doing in my project, a custom json parser to serialize/deserialize into features or geojson. Sadly there's no convenience method to do all of this. – philoez98 Jun 13 '19 at 12:39