Here is a simple solution
Since Google does not give reliable results regarding coordinates that lie on ocean or inland waters, you need to use another backup service, such as Yandex, to help provide this important information when it is missing. Most likely, you will not want to use Yandex as the main geocoder, since Google far surpasses the reliability and completeness of world data, however Yandex can be very useful for retrieving data when it is associated with coordinates over bodies of water, so use both.
Yandex Documentation: https://api.yandex.com.tr/maps/doc/geocoder/desc/concepts/input_params.xml
Steps for extracting the name of the ocean:
1.) First use Google to change the geocode coordinate.
2.) If Google returns zero results, the probability that the coordinate lies above the ocean is 99%. Now make a secondary reverse geocoding request with the same coordinates as for Yandex. Yandex will return a JSON response with exact coordinates, inside this answer there will be two "key": values ​​"value" of importance
["GeoObject"]["metaDataProperty"]["GeocoderMetaData"]["kind"] and ["GeoObject"]["name"]
Check the view key, if it is == "hydro", you know that you are above the body of the water, and since Google returned zero results, it is probably 99.99%, this body of water is the ocean. The name of the ocean will be designated as "name".
Here is an example of how I use this strategy written in Ruby
if result.data["GeoObject"]["metaDataProperty"]["GeocoderMetaData"]["kind"] == "hydro" ocean = result.data["GeoObject"]["name"] end
Steps to extract the name of the inner body of water:
In this example, suppose our coordinate lies somewhere in the lake:
1.) First use Google to change geocoding coordinates.
2.) Google is likely to return a result that is an outstanding default address on land. As a result, it supplies the coordinates of the returned address, this coordinate will not match the one you specified. Measure the distance between the coordinate you submitted and the one returned with the result, if it differs significantly (for example, 100 yards), then query the secondary backup using Yandex and check to see the “view” value, if it is “hydro”, then you know that coordinate lies on the water. Since Google returned the result, unlike the above example, this is 99.99%, it is probably the internal water flow of water, so now you can get the name. If the “view” is not “hydro,” use a Google geocoded object.
["GeoObject"]["metaDataProperty"]["GeocoderMetaData"]["kind"] and ["GeoObject"]["name"]
Here is the same code written in Ruby to get inland_body_of_water
if result.data["GeoObject"]["metaDataProperty"]["GeocoderMetaData"]["kind"] == "hydro" inland_body_of_water = result.data["GeoObject"]["name"] end
Licensing note: as far as I know, Google does not allow its data to be displayed on any maps other than those offered by Google. However, Yandex has very flexible licensing, and you can use their data to display on Google maps.
In addition, Yandex has a limit on the maximum speed of 50,000 requests / day for free and without an API key.