Search code examples
androidgoogle-maps

Check land or water in google map


I am generating random latitude and longitude. But unable to find land or water for random lat and longitude. Checked with google API, I am not finding and I searched in google. But I cannot find solution, please help me. I used this source (https://onwater.io/users/sign_up )url. It gives only 15 request per minute. I need 50 lat and long to check whether it is land or water.


Solution

  • For today easiest way to do this is to use Google Static Map API and Styled Maps: you can create request for just one pixel map (size=1x1) for your Lat/Lon coords with hidden all features except water and set water color to e.g. pure blue (0x0000FF). Something like that

    https://maps.googleapis.com/maps/api/staticmap?&center={YOUR_LAT},{YOUR_LON}&zoom={YOUR_ZOOM}&size=1x1&style=feature:all|visibility:off&style=feature:water|visibility:on&style=feature:water|color:0x0000FF&key={YOUR_API_KEY}

    Then download it (like in this answer of Vivek Khandelwal):

    ...
    
    public static Bitmap getGoogleMapThumbnail(double lati, double longi) {
        String URL = "http://maps.google.com/maps/api/staticmap?center=" + lati + "," + longi + "&zoom=15&size=200x200&sensor=false";
        Bitmap bmp = null;
        bmp = getBitmapFromURL(URL);
        return bmp;
    }
    
    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    ...
    

    and test color of one pixel like in that answer of Raz:

    ...
    int pixel = bitmap.getPixel(x,y);
    if (pixel == Color.BLUE) {
       // water!
    }
    ...
    

    if it blue (0x0000FF) - there is water on your Lat/Lon coordinates.

    NB! You should set appropriate zoom level and remember that is no map tiles for all zoom levels for entire Earth. Also some water/earth can be missed on map. So, that is not 100% right solution, but hope it helps.