Check if user is in forest using location

I would like to write an application that can determine if a user is in the forest or not. Is there any way to do this using Android?

What I tried:

  • It uses the Google Places API and hopes to get the place type "natural_feature" when the user is in the forest. Sorry, I get "ZERO_RESULTS" most of the time

  • Read about WMS layers that show areas of the forest and tried to put one on top of GoogleMap. I couldn’t get it working, and also noticed that, most likely, I can show the map without checking if the user is in one of the areas of the layer ...

Is there a way to check if the current position is in the green area on Google maps? This may even be enough if all else fails.

Thanks in advance!

+4
source share
1 answer

I currently don't know if there is an easier way to do this, but you can try the following:

  • Get a snapshot from your card.
  • Get the average color of the snapshot
  • Compare the middle color with the predefined forest color
  • If their difference is below your accepted tolerance, then you are in the green area map.

The result may not be accurate if you do not zoom in on the map and the green area is too small. In this case, you can do your calculations only at certain zoom levels.

        final int forestR = 214;
        final int forestG = 233;
        final int forestB = 215;
        final int tolerance = 60;

        Button button = (Button) findViewById(R.id.check_forest_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {


                    @Override
                    public void onSnapshotReady(Bitmap snapshot) {
                        int pixelCount = 0;
                        int redColors = 0;
                        int greenColors = 0;
                        int blueColors = 0;
                        for (int y = 0; y < snapshot.getHeight(); y++) {
                            for (int x = 0; x < snapshot.getWidth(); x++) {
                                int c = snapshot.getPixel(x, y);
                                pixelCount++;
                                redColors += Color.red(c);
                                greenColors += Color.green(c);
                                blueColors += Color.blue(c);
                            }
                        }
                        // calculate average of bitmap r,g,b values
                        int red = (redColors / pixelCount);
                        int green = (greenColors / pixelCount);
                        int blue = (blueColors / pixelCount);

                        int difference = Math.abs(red - forestR) + Math.abs(green - forestG) + Math.abs(blue - forestB);

                        boolean isForest = difference < tolerance;//use this to display your information
                    }
                };

                map.snapshot(callback);
            }
        });
+1

Source: https://habr.com/ru/post/1662238/


All Articles