Basically I have set of points [lat-long pairs] which define different polygons on a map. Now I have a coordinate [lat-long] from which I want to find the nearest polygon from that point.
[GIS] How to find the nearest polygon from a given point
geometrygoogle mapsgoogle-maps-api
Related Solutions
Look at the "DISTANCE POINT TO POLYLINE OR POLYGON Demo" at:
[This is a Google Maps V2 not v3 demo]
http://www.bdcc.co.uk/Gmaps/BdccGmapBits.htm
SO if you have a polygon/polyline of the sea you can quickly find the distance from a point in the sea - it the reverse of this demo. Data (Coastline) can be downloaded and added to Fusion Tables http://www.ngdc.noaa.gov/mgg/shorelines/gshhs.html
"This uses some 3D geometry maths to work out the distance between the mousepointer's lat/lng and a GPolyline (or border of a GPolygon). The distance in metres is displayed in a panel on the bottom right of the map.
You can test this by panning the map so that a vertex of the polyline lies on one end of the scale bar and then moving the mousepointer to the other end of the scale bar. Also, if you zoom well in, then the distance should stay around 1m or less as you move the mousepointer all along the polyline. The test polyline has horizontal and vertical sides of 2km so the furthest away you can get inside its square is 1000m.
Source code for the distance calculation is available too.
This code can be used for determining how far 'Off Route' a point is or for filtering 'Points Of Interest' for proximity to a route. I have used it with the GSHHS data set to find out how far it is to the sea.
There is a similar, better demo here http://wtp2.appspot.com/cSnapToRouteDemo.html
Not sure if you're going to be able to use the Google Maps api to do this. You more likely need a bespoke gis solution, by which I mean probably a spatial database which stores the bus routes and then works out firstly the nearest point on the route (which probably involves firstly finding the nearest route, then the nearest node on that route), otherwise with a large network you would have to iterate through thousands of route points.
The next task is to work out whether it is nearer along the line to one stop than the other stop. This is fairly easily done, so long as you have access to the underlying line geometry.
If you can get the route geometry in Gmaps there may be a way of iterating through the line segments and doing various "nearest" calculations on them, but the Google Maps API is quite a long way from real spatial database functionality, ad would probably involve a lot of API calls, unless you can implement your own solution for things like converting lat/long to distances on the ground.
Otherwise, you're stuck using the less accurate method.
Best Answer
There are two approaches. One is brute-force: write code to find the distance between a point and a polygon, and then iterate over all polygons. The second involves building efficient data structures, an arduous process.
For the first approach, you might look at this Wolfram Demonstration Project for computing the distance from a point to a polygon. Link here; image below. Note that sometimes the min distance is achieved at a point interior to an edge of the polygon.