I need to know the predefined point is in the sea or on the land [duplicate]

心不动则不痛 提交于 2019-11-28 04:45:12

You can use the Google Maps Geocode API.

If your address is in land, the result_type of the response will be something like "administrative_area". if you are in the sea, the response will be "natural_feature".

Here are two examples:

Edit: Some more examples in response to comments:

I have thought about this once before.

I would not use a web service. but I would use a pretty simple image.

This image is taken from http://www.vectorworldmap.com/vectormaps/vector-world-map-v2.2-blank.jpg

EDIT from a great comment

I like the solution, but I'm not sure that map is totally accurate or equirectangular (which would be the easiest to work with). How about this one? naturalearth.springercarto.com/ne3_data/8192/masks/water_8k.png

//END

  • I would map lat/long over this map for know points so you know your map is correct.
  • For any give point, I would locate the pixel location on this map and figure out the colour (php can do this).
  • If the colour is white, I would say it is sea, black = land.
  • if you dont want lakes, open the map up and color all the lakes black.

If you need super high res maps, I would get a very large map and divide it up in a grid of smaller images that you can load.

If you need to make this system super duper fast. I would can this image pixel for pixel and create a database of this stuff so you can look it up pretty quick. But already this image below actually acts as an excellent database that can be easily checked or changed.

Did you want oceans and lakes?

Curious to know where you are going with this, as I like this problem.

John.

tomcooks

Expanding on John Ballinger, i have successfully used this trick:

  • Get a 1x1 pixel google image static map centered on your coordinates
  • Style map to black (land) and white (water) via Google Maps API styling
  • Find whether the pixel is black or white

In php:

$lat = your latitude
$lon = your longitude
$im = imagecreatefrompng('http://maps.googleapis.com/maps/api/staticmap?center='.$lat.','.$lon.'&zoom=21&format=png&sensor=false&size=1x1&maptype=roadmap&style=feature:administrative|visibility:off&style=feature:landscape|color:0x000000&style=feature:water|color:0xffffff&style=feature:road|visibility:off&style=feature:transit|visibility:off&style=feature:poi|visibility:off');
//get pixel color, put it in an array
$color_index = imagecolorat($im, 0, 0);
$color_tran = imagecolorsforindex($im, $color_index);

//if, for example, red value of the pixel is 0 we are on land
if($color_tran['red']==0){
    //do your thing
    echo "we are on land";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!