GeoCoding in Rails for polygon bounds [closed]

久未见 提交于 2019-12-04 23:54:29

问题


Is there a way in rails via some mapping API/gem where I can define map areas/map zones (with polygon bounds) and detect if an address falls within a particular zone/area.


回答1:


This is easily done with geokit.

First construct a polygon from LatLng:

polygon = Geokit::Polygon.new([ 
  Geokit::LatLng.new(0,0), 
  Geokit::LatLng.new(10,0), 
  Geokit::LatLng.new(10,10), 
  Geokit::LatLng.new(20,10), 
  Geokit::LatLng.new(20,0), 
  Geokit::LatLng.new(30,0), 
  Geokit::LatLng.new(30,20), 
  Geokit::LatLng.new(0,20)
])

Then you can test if the polygon contains a LatLng:

point = Geokit::LatLng.new(5,5)

polygon.contains?(point) # => true


来源:https://stackoverflow.com/questions/25275005/geocoding-in-rails-for-polygon-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!