Getting coordinates from GeoJson









up vote
0
down vote

favorite












I have geojson layer in my app. It's being loaded dynamically. Basically, I need to add marker to all geojson features so I need "position".



I have loop like this :



 for (GeoJsonFeature feature : layer.getFeatures()) 
feature.setPolygonStyle(polygonStyle);



But I have no idea how to get coordinates from feature. I've tried this method :



private List<LatLng> getCoordinatesFromGeometry(Geometry geometry) 

List<LatLng> coordinates = new ArrayList<>();

// GeoJSON geometry types:
// http://geojson.org/geojson-spec.html#geometry-objects

switch (geometry.getGeometryType())
case "Point":
coordinates.add(((GeoJsonPoint) geometry).getCoordinates());
break;
case "MultiPoint":
List<GeoJsonPoint> points = ((GeoJsonMultiPoint) geometry).getPoints();
for (GeoJsonPoint point : points)
coordinates.add(point.getCoordinates());

break;
case "LineString":
coordinates.addAll(((GeoJsonLineString) geometry).getCoordinates());
break;
case "MultiLineString":
List<GeoJsonLineString> lines =
((GeoJsonMultiLineString) geometry).getLineStrings();
for (GeoJsonLineString line : lines)
coordinates.addAll(line.getCoordinates());

break;
case "Polygon":
List<? extends List<LatLng>> lists =
((GeoJsonPolygon) geometry).getCoordinates();
for (List<LatLng> list : lists)
coordinates.addAll(list);

break;
case "MultiPolygon":
List<GeoJsonPolygon> polygons =
((GeoJsonMultiPolygon) geometry).getPolygons();
for (GeoJsonPolygon polygon : polygons)
for (List<LatLng> list : polygon.getCoordinates())
coordinates.addAll(list);


break;


return coordinates;



However it doesn't seems to work. Any help, please ?










share|improve this question























  • "But I have no idea how to get coordinates from feature. " Referring to http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/geojson/GeoJsonFeature.html a GeoJSONFeature has the getGeometry() method which gives you a GeoJsonGeometry which is one of the geometry types like a point, line, multiline etc. and you already have the code for extracting the individual coordinate points from those. So, what is the problem?
    – Markus Kauppinen
    2 days ago







  • 1




    I suppose you want just one marker for each feature. Then of course extracting all the coordinate points is a starting point but not enough. You need to decice how you want to decide the marker location for a feature. You could have a look at calculating the bounding box for a feature and then calculating the middle point of the bounding box. I'm not sure this is the solution, but at least something to consider.
    – Markus Kauppinen
    2 days ago










  • Of course if the GeoJSON features are quite small (in relation to expected map zoom levels) it might be enough to just pick one of the coordinate points e.g. the first one and then there's no point in anything more elaborate.
    – Markus Kauppinen
    2 days ago















up vote
0
down vote

favorite












I have geojson layer in my app. It's being loaded dynamically. Basically, I need to add marker to all geojson features so I need "position".



I have loop like this :



 for (GeoJsonFeature feature : layer.getFeatures()) 
feature.setPolygonStyle(polygonStyle);



But I have no idea how to get coordinates from feature. I've tried this method :



private List<LatLng> getCoordinatesFromGeometry(Geometry geometry) 

List<LatLng> coordinates = new ArrayList<>();

// GeoJSON geometry types:
// http://geojson.org/geojson-spec.html#geometry-objects

switch (geometry.getGeometryType())
case "Point":
coordinates.add(((GeoJsonPoint) geometry).getCoordinates());
break;
case "MultiPoint":
List<GeoJsonPoint> points = ((GeoJsonMultiPoint) geometry).getPoints();
for (GeoJsonPoint point : points)
coordinates.add(point.getCoordinates());

break;
case "LineString":
coordinates.addAll(((GeoJsonLineString) geometry).getCoordinates());
break;
case "MultiLineString":
List<GeoJsonLineString> lines =
((GeoJsonMultiLineString) geometry).getLineStrings();
for (GeoJsonLineString line : lines)
coordinates.addAll(line.getCoordinates());

break;
case "Polygon":
List<? extends List<LatLng>> lists =
((GeoJsonPolygon) geometry).getCoordinates();
for (List<LatLng> list : lists)
coordinates.addAll(list);

break;
case "MultiPolygon":
List<GeoJsonPolygon> polygons =
((GeoJsonMultiPolygon) geometry).getPolygons();
for (GeoJsonPolygon polygon : polygons)
for (List<LatLng> list : polygon.getCoordinates())
coordinates.addAll(list);


break;


return coordinates;



However it doesn't seems to work. Any help, please ?










share|improve this question























  • "But I have no idea how to get coordinates from feature. " Referring to http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/geojson/GeoJsonFeature.html a GeoJSONFeature has the getGeometry() method which gives you a GeoJsonGeometry which is one of the geometry types like a point, line, multiline etc. and you already have the code for extracting the individual coordinate points from those. So, what is the problem?
    – Markus Kauppinen
    2 days ago







  • 1




    I suppose you want just one marker for each feature. Then of course extracting all the coordinate points is a starting point but not enough. You need to decice how you want to decide the marker location for a feature. You could have a look at calculating the bounding box for a feature and then calculating the middle point of the bounding box. I'm not sure this is the solution, but at least something to consider.
    – Markus Kauppinen
    2 days ago










  • Of course if the GeoJSON features are quite small (in relation to expected map zoom levels) it might be enough to just pick one of the coordinate points e.g. the first one and then there's no point in anything more elaborate.
    – Markus Kauppinen
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have geojson layer in my app. It's being loaded dynamically. Basically, I need to add marker to all geojson features so I need "position".



I have loop like this :



 for (GeoJsonFeature feature : layer.getFeatures()) 
feature.setPolygonStyle(polygonStyle);



But I have no idea how to get coordinates from feature. I've tried this method :



private List<LatLng> getCoordinatesFromGeometry(Geometry geometry) 

List<LatLng> coordinates = new ArrayList<>();

// GeoJSON geometry types:
// http://geojson.org/geojson-spec.html#geometry-objects

switch (geometry.getGeometryType())
case "Point":
coordinates.add(((GeoJsonPoint) geometry).getCoordinates());
break;
case "MultiPoint":
List<GeoJsonPoint> points = ((GeoJsonMultiPoint) geometry).getPoints();
for (GeoJsonPoint point : points)
coordinates.add(point.getCoordinates());

break;
case "LineString":
coordinates.addAll(((GeoJsonLineString) geometry).getCoordinates());
break;
case "MultiLineString":
List<GeoJsonLineString> lines =
((GeoJsonMultiLineString) geometry).getLineStrings();
for (GeoJsonLineString line : lines)
coordinates.addAll(line.getCoordinates());

break;
case "Polygon":
List<? extends List<LatLng>> lists =
((GeoJsonPolygon) geometry).getCoordinates();
for (List<LatLng> list : lists)
coordinates.addAll(list);

break;
case "MultiPolygon":
List<GeoJsonPolygon> polygons =
((GeoJsonMultiPolygon) geometry).getPolygons();
for (GeoJsonPolygon polygon : polygons)
for (List<LatLng> list : polygon.getCoordinates())
coordinates.addAll(list);


break;


return coordinates;



However it doesn't seems to work. Any help, please ?










share|improve this question















I have geojson layer in my app. It's being loaded dynamically. Basically, I need to add marker to all geojson features so I need "position".



I have loop like this :



 for (GeoJsonFeature feature : layer.getFeatures()) 
feature.setPolygonStyle(polygonStyle);



But I have no idea how to get coordinates from feature. I've tried this method :



private List<LatLng> getCoordinatesFromGeometry(Geometry geometry) 

List<LatLng> coordinates = new ArrayList<>();

// GeoJSON geometry types:
// http://geojson.org/geojson-spec.html#geometry-objects

switch (geometry.getGeometryType())
case "Point":
coordinates.add(((GeoJsonPoint) geometry).getCoordinates());
break;
case "MultiPoint":
List<GeoJsonPoint> points = ((GeoJsonMultiPoint) geometry).getPoints();
for (GeoJsonPoint point : points)
coordinates.add(point.getCoordinates());

break;
case "LineString":
coordinates.addAll(((GeoJsonLineString) geometry).getCoordinates());
break;
case "MultiLineString":
List<GeoJsonLineString> lines =
((GeoJsonMultiLineString) geometry).getLineStrings();
for (GeoJsonLineString line : lines)
coordinates.addAll(line.getCoordinates());

break;
case "Polygon":
List<? extends List<LatLng>> lists =
((GeoJsonPolygon) geometry).getCoordinates();
for (List<LatLng> list : lists)
coordinates.addAll(list);

break;
case "MultiPolygon":
List<GeoJsonPolygon> polygons =
((GeoJsonMultiPolygon) geometry).getPolygons();
for (GeoJsonPolygon polygon : polygons)
for (List<LatLng> list : polygon.getCoordinates())
coordinates.addAll(list);


break;


return coordinates;



However it doesn't seems to work. Any help, please ?







android geojson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









MrUpsidown

14.5k64893




14.5k64893










asked 2 days ago









Bartos

214316




214316











  • "But I have no idea how to get coordinates from feature. " Referring to http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/geojson/GeoJsonFeature.html a GeoJSONFeature has the getGeometry() method which gives you a GeoJsonGeometry which is one of the geometry types like a point, line, multiline etc. and you already have the code for extracting the individual coordinate points from those. So, what is the problem?
    – Markus Kauppinen
    2 days ago







  • 1




    I suppose you want just one marker for each feature. Then of course extracting all the coordinate points is a starting point but not enough. You need to decice how you want to decide the marker location for a feature. You could have a look at calculating the bounding box for a feature and then calculating the middle point of the bounding box. I'm not sure this is the solution, but at least something to consider.
    – Markus Kauppinen
    2 days ago










  • Of course if the GeoJSON features are quite small (in relation to expected map zoom levels) it might be enough to just pick one of the coordinate points e.g. the first one and then there's no point in anything more elaborate.
    – Markus Kauppinen
    2 days ago

















  • "But I have no idea how to get coordinates from feature. " Referring to http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/geojson/GeoJsonFeature.html a GeoJSONFeature has the getGeometry() method which gives you a GeoJsonGeometry which is one of the geometry types like a point, line, multiline etc. and you already have the code for extracting the individual coordinate points from those. So, what is the problem?
    – Markus Kauppinen
    2 days ago







  • 1




    I suppose you want just one marker for each feature. Then of course extracting all the coordinate points is a starting point but not enough. You need to decice how you want to decide the marker location for a feature. You could have a look at calculating the bounding box for a feature and then calculating the middle point of the bounding box. I'm not sure this is the solution, but at least something to consider.
    – Markus Kauppinen
    2 days ago










  • Of course if the GeoJSON features are quite small (in relation to expected map zoom levels) it might be enough to just pick one of the coordinate points e.g. the first one and then there's no point in anything more elaborate.
    – Markus Kauppinen
    2 days ago
















"But I have no idea how to get coordinates from feature. " Referring to http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/geojson/GeoJsonFeature.html a GeoJSONFeature has the getGeometry() method which gives you a GeoJsonGeometry which is one of the geometry types like a point, line, multiline etc. and you already have the code for extracting the individual coordinate points from those. So, what is the problem?
– Markus Kauppinen
2 days ago





"But I have no idea how to get coordinates from feature. " Referring to http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/geojson/GeoJsonFeature.html a GeoJSONFeature has the getGeometry() method which gives you a GeoJsonGeometry which is one of the geometry types like a point, line, multiline etc. and you already have the code for extracting the individual coordinate points from those. So, what is the problem?
– Markus Kauppinen
2 days ago





1




1




I suppose you want just one marker for each feature. Then of course extracting all the coordinate points is a starting point but not enough. You need to decice how you want to decide the marker location for a feature. You could have a look at calculating the bounding box for a feature and then calculating the middle point of the bounding box. I'm not sure this is the solution, but at least something to consider.
– Markus Kauppinen
2 days ago




I suppose you want just one marker for each feature. Then of course extracting all the coordinate points is a starting point but not enough. You need to decice how you want to decide the marker location for a feature. You could have a look at calculating the bounding box for a feature and then calculating the middle point of the bounding box. I'm not sure this is the solution, but at least something to consider.
– Markus Kauppinen
2 days ago












Of course if the GeoJSON features are quite small (in relation to expected map zoom levels) it might be enough to just pick one of the coordinate points e.g. the first one and then there's no point in anything more elaborate.
– Markus Kauppinen
2 days ago





Of course if the GeoJSON features are quite small (in relation to expected map zoom levels) it might be enough to just pick one of the coordinate points e.g. the first one and then there's no point in anything more elaborate.
– Markus Kauppinen
2 days ago


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225164%2fgetting-coordinates-from-geojson%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225164%2fgetting-coordinates-from-geojson%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo