How to find point along line defined by two points? - google-maps

There is a function in google maps geometry library
interpolate(from:LatLng, to:LatLng, fraction:number)
which finds a point between two other points:
A--X--B
I need something very similar, find a point further along the line:
A--B--X
Interpolate does not accept fraction>1 so I cannot use it. Is there some simple way to calculate the point?
I want to achieve given distance between A and X points.
EDIT: In my application the distance was really small so I used fromLatLngToPoint conversion and linear interpolation, which get good enough results on small area.

You can calculate bearing, then find destination point with code from this excellent page

Related

Calculate shortest distance between a GPS coordinate and a route

Is there a simple way to calculate the closest distance between a route (directions between two cities for example) and a GPS coordinate using the Google Maps API?
One appoach I can think of is to translate the route into a set of GPS coordinates and calculate the distance to each coordinate. Is there a better way of doing this?
One appoach I can think of is to translate the route into a set of GPS
coordinates and calculate the distance to each coordinate. Is there a
better way of doing this?
Yes there is a much better way.
The first part is correct: translate to a polyline of lat/lon coordinates.
However the second is to simple. You want the shortest distance to the line segment not the the next corner point, that is the start or end of the line.
In school you used the hessian normal distance. However this formula calculates just the distance to an infinite line. In case of a polyline you have an sequence of line segments.
So you need a formula for "distance to line segment". This one you can find using that search terms.
Having this formula implemented, you interate over all line segments each defined by two points (polyPoint[i], polyPoint[i+1]) and take the minimum of the distance.
Don't forget to transform the current line segement to cartesian (x,y) space, because the usualy formulas, do not work on spehrical coordinates.
This approach is more complex, but gives exact results. Remember a line on a route if often 400-700 meters long. So the simple approach you asked, gives in this case an error of 200-350m if you are in the middle between the two points.

GPS coordinations for each kilometer

I'll find a route between two places, for example using google maps. I'd like to divide the route to kilometers (two following places will be at a distance of 1 km), and get GPS coordinations of these places. This is because then I'll be able to get exacly the coordinations of, for example, 5th kilometer on the route. Could you please advice me how to achieve it?
This is extremely nontrivial. Is say your best bet is to find an algorithm to load the bearing between two points, then one to load a coordinate given a start point, distance, and bearing. This could give you it, but only if the data contained only straight lines. Since I assume the Google Maps API only gives you the turns the user has to make, this approach will be inaccurate when there are bends in roads. You'd need GIS data for roads and what will undoubtedly turn into a complicated algorithm to find something like this. It's definitely doable, but that's l how I'd start. Look into the Census TIGER road data, it should help.
Unless, of course, I'm wrong and the API does actually give enough points to cleanly map it, in which case those functions should be easy to find and implement.
This will only work if you have the polyline as a sequence of lat/lon (or other) coordinates, wherever you get that from.
Then you start at the beginning an iterate through the lines (point[i], point[i+1]).
THis distance you calculate with standard API.
while itersting you sum up the distance.
Once you exceed the 1000m, you know that the splitting point (the 1000m marker) is at line segment [i,i+1].
To calculate the exact position where on the line that is, you take the total summed meters from previous segment, and the value of this segment and do a linear interpolation.
The working code is a bit complexer: there can be multiple markes within one segement.
But first find out where you get the polyline from, whitou that it will not work.

Google Maps/OSM - Finding records within 100 miles of an area (not a point)

I would like to find all points that are within N miles of a given area.
E.g. the area is California: Find all points that are within 50 miles of the border of California (not the middle of California).
When using Google Maps the distance is calculated using 'the middle' of the given location, but I need to calculate the distance using the borders of the given location. The location could be any zip code, city or country.
Could that be done by drawing a polygon using California's coordinates on a map and calculate the distance to location B using the points of the polygon?
Is there a more elegant solution to this? Any ideas?
Thanks!
I'm not sure if I understand your requirements completely, but I will give it a try with different interpretations:
1. You want to filter own map points:
This can be done with any GIS or a own service that offers a call like my_points_in_area(bbox). Bbox means here boundingbox and is the 2x lat/lon pair describing the rectangle around your given centerpoint. If you want to be accurate and really just deliver whats within 100km, you might need to test the distance to the POIs once more, as the rectangle will also include points that are a bit more far away.
2. You want to filter OSM data:
You might use a reverse-geocoding service as Nominatim to get informations about points of interests that are within this distance: http://wiki.openstreetmap.org/wiki/Nominatim
Otherwise import the OSM data using osmosis to a PostGIS DB. AFAIK there is (currently) no DB tool for Oracle: http://wiki.openstreetmap.org/wiki/Oracle
I'm sorry if I missed your question, but then please add more details :)

Mysql Distance from test point to a polygon

I have stored some map zones to a table using Geometry type field.
So the inserts are like this:
INSERT INTO zones (zoneName, coords) VALUES ('name',
PolygonFromText('POLYGON((
41.11396418691335 1.2562662363052368,
41.11370552595821 1.2560248374938965,
41.11851079510035 1.2459397315979004,
41.11880984984478 1.2461864948272705,
41.11396418691335 1.2562662363052368))'));
Then I have the user position, and I need to know if he is inside some zone. This works well with this:
SELECT id
FROM zones
WHERE MBRContains(coords,GeomFromText('POINT(41.117783 1.260590)'))
But sometimes, user position is not perfect, so I think its better to know wich zone is closest to user position.
That is the part that I don't have any idea about... I found some queries to get distance between two points, but not a point and polygons.
The MBR series of functions (like MBRContains) are unsuitable for what you're trying to do; they only test bounding rectangle inclusion.
You may wish to jump forward to MySQL 5.6.1, and use the ST_ functions, like ST_Contains. These functions actually test the geometry.
The problem you're working on can be defined as an uncertainty in the position of your POINT when you go to compare it to your collection of boundary POLYGON items.
Try this: create a POLYGON from your point that is a square with the size of your uncertainty. You can think of this square as a "fuzzy" point. (You could also use an octagon or another closer approximation of a circle in place of a rectangle, but your querying speed will slow.)
Then use ST_Within to see if you have a unique polygon that entirely contains your fuzzy point. If you get just one polygon, you're done.
If you get multiple polygons that entirely contain your fuzzy point, that means some of your boundary polygons overlap other ones. You need to figure out what this means in your problem space. If your data is intended to be properly structured cartographic boundary data, it means you have a data mistake. (NOTE: This is not unheard of :-)
If you get no polygons that entirely contain your fuzzy point, then your fuzzy point may or may not overlap the boundary of at least one polygon. Use ST_Overlaps to find those polygons.
If you get just one, you're done -- your fuzzy point is near the boundary of just one polygon.
If you get none, you're done -- your fuzzy point is away from the boundaries of all your polygons.
If you get more than one hit, you have an ambiguity -- your fuzzy point is near the boundary of more than one polygon.
This is the hard case to sort out. You could reduce the size of the fuzzy point and try again. This MIGHT yield just one polygon result. But, you could deceive yourself into thinking that your points are more accurate than they are by doing this.
MySQL doesn't have the geometric operator Area(Intersection(Polygon, FuzzyPoint)). If it did you could choose the polygon with the biggest area of intersection with your fuzzy point, and that would be a good disambiguator. But it would still be as inaccurate as the position of your point.
Maybe your application should handle the category of result "too near the boundary of A, B, and C to be sure."

MBR Within not accurate in mysql

I have been using the MBRWithin function for quite a lot of times. Suddenly I notice on google map this POINT(101.11857 4.34475) is out of the geo fence which I specify but it still give a value of 1 in mysql any reason or tweaking need to be done?
SELECT MBRWithin(GeomFromText('POINT(101.11857 4.34475)'),GeomFromText('POLYGON((101.12112522125244 4.3531723687957164,101.11846446990967 4.351417913665312,101.13138198852539 4.336397898951581,101.13477230072021 4.33211863778494,101.14065170288086 4.321933898868271,101.14992141723633 4.306699328215635,101.15455627441406 4.30978050198082,101.1397933959961 4.334600612212089,101.12112522125244 4.3531723687957164,101.12112522125244 4.3531723687957164))')) As geoFenceStatus
MySQL 5.6.1 and later have exact geometry algorithms in addition to the earlier functions that only operated on MBR.
You can use ST_WITHIN rather than MBR_WITHIN. See documentation. Like this
SELECT ST_Within(GeomFromText('POINT(101.11857 4.34475)'),
GeomFromText('POLYGON((101.12112522125244 4.3531723687957164,101.11846446990967
4.351417913665312,101.13138198852539 4.336397898951581,101.13477230072021
4.33211863778494,101.14065170288086 4.321933898868271,101.14992141723633
4.306699328215635,101.15455627441406 4.30978050198082,101.1397933959961
4.334600612212089,101.12112522125244 4.3531723687957164,101.12112522125244
4.3531723687957164))')) As geoFenceStatus
MBRWithin() will return results based on the minimum bounding rectangle of it's parameters. Your polygon contains both larger and smaller values for both coordinates than the point, so it will be within the polygon's MBR.
MySQL has no built-in point in polygon algorithm, so you'll either have to roll your own or find one elsewhere. This one seems to be a good candidate.