treisto
treisto

Reputation: 11

Extract coordinates lat lng from geojson feature collection in java/Android Studio

I'm new to java, new to android, and I'm trying to create a google maps android app that would use the city's bus system API to search for bus stops, real-time departures,..

The API returns a GeoJSON file like this:

{

"type": "FeatureCollection",
"features": [
    {
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [6.113204, 49.61028]
        },
        "properties": {
            "id": 200403005,
            "name": "Belair, Sacré-Coeur"
        }
    },
    ...
]

}

In fact the file is really large - all the bus stops in the city.

I need to find the closest bus stop to user's gps location.

In order to do this I intend to extract all the coordinates: "coordinates": [6.113204, 49.61028] parse them and find which one is closer to user position.

Google documentation, and other documentation I could find about this is scarce and not beginner friendly...

In Android studio/java/google maps there is a "feature.getGeometry()" thing, but I can't go deeper, to the coordinates' level and get them.

This is what i'm trying to do:

 // Iterate over all the features stored in the layer
 for (GeoJsonFeature feature : layer.getFeatures()) {
     // Check if the feature is what i need
     if (feature.getProperty("id") != null && feature.hasProperty("name")) {
            //Here I need to get the coordinates and create an array which i can use further to find the closest bus stop to user position like below (or similar).

Apparently there is nothing that goes beyond "Geometry" level, to coordinates?

I mean, I see I can do "feature.getProperty("id")" and use it, but apparently i can't do "feature.getGeometry("coordinates")", or something like this?

Code to find closest bus stop:

for (int i = 0; i < jsonArray_buses.length(); i++) {
            JSONObject jsonObject = jsonArray_buses.getJSONObject(i);
            double lng = Double.parseDouble(jsonArray_buses.getJSONObject(i).getString("longitude"));
            double lat = Double.parseDouble(jsonArray_buses.getJSONObject(i).getString("latitude"));

            Log.e("TAG_latlong", "latitude:" + latitude + " longitude:" + longitude + "marker_lat:" + lat + "marker_long:" + lng);

            Location locationA = new Location("point A");
            locationA.setLatitude(lat);
            locationA.setLongitude(lng);

            Location locationB = new Location("point B");
            locationB.setLatitude(latitude);
            locationB.setLongitude(longitude);


            float min_distance_old = min_distance;
            min_distance = min(min_distance, locationA.distanceTo(locationB));

            if (min_distance_old != min_distance) {
                closest = i;
            }

         } //end for


        JSONObject display_jsonObject = jsonArray_buses.getJSONObject(closest);
        //save
        double lng = Double.parseDouble(jsonArray_buses.getJSONObject(closest).getString("longitude"));
        double lat = Double.parseDouble(jsonArray_buses.getJSONObject(closest).getString("latitude"));
        markerList.add(mMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA))
                .title(display_jsonObject.getString("name"))
                .snippet(Integer.toString((display_jsonObject.getInt("id"))))
                .position(new LatLng(lat, lng))));

Thank you very much.

Upvotes: 0

Views: 2674

Answers (1)

samnursa
samnursa

Reputation: 51

  1. you need to add this code bellow in gradle between dependencies

dependencies { compile 'com.google.maps.android:android-maps-utils:0.5+' }

  1. add this code in onCreate

GeoJsonLayer layer = new GeoJsonLayer(mMap, jObjek); //mMap is GoogleMap object and jObjek is your geojson, if you take geojson from API try to use volley

  1. add GeoJsonLayer to map

layer.addLayerToMap();

hope this help.

Upvotes: 1

Related Questions