Get distance to walk in Google Maps - javascript

I'm trying to do some experiment with an app. I want that, given my location and given a distance, I get a point in Google Map to which I could walk that distance.
That has some complications since getting a point X miles out from you is "easy" but it's unlikely you'll actually walk that amount of miles unless you can actually go in a straight line.
The approach I followed is using google.maps.places.PlacesService and the function nearbySearch given a radius. Then I process the distance to each returned point and luckily I get one point I could walk that distance far.
However, I've been trying some edge cases, like picking a road near a desert and then it won't work cause "places" are points of interest and that may be only just a plain road with nothing else than boring bushes for a long, long way.
In my mind it just should follow that road for the given distance and then stop.
To be clear:
I want to know from my starting point, which would be my ending point if I were to walk 1 Km
Do you have any better approach to this kind of situations?

The google.maps.DirectionsService class will calculate the route a traveller would take, given an origin and a destination. It accepts a google.maps.DirectionsRequest object. The DirectionsRequest has an option called TravelMode, which will accept any of four constants (BICYCLING, DRIVING, TRANSIT, WALKING) from the google.maps.TravelMode class.
To find a point, say, 3 kilometers from your location, I would:
Request walking directions from DirectionsService, using your current location and a point approximately 3 kilometers away from you.
Iterate over the overview_path, continually adding the length of the current segment to your grand total.
Cut the last segment to length when you exceed 3 kilometers, creating a length whose final length is very close to 3 kilometers.
I made you a Plunk demonstrating how this could be done.

Related

If I have a GeoJSON file of a certain city's wards, how would I figure out which ward I'm in based on my lat/long coordinates?

Apologies in advance if this might seem like a silly question with an easy answer, or if its too vague, but I really am very new to the whole Geocoding shabang so I might not be able to ask the best questions yet.
Let's say I have the GeoJSON file for Philadelphia (https://hub.arcgis.com/datasets/phl::political-wards/explore?location=39.965479%2C-75.035211%2C11.78). I input a pair of coordinates for the Philadelphia City Hall, for example:
Latitude: 39.953306°
Longitude: -75.163577°. What would I need to do to get back the exact ward those coordinates lie in? In JavaScript (Node.js) ideally but any language would work as long as I understand the logic and steps I'd need to take.
Also, feel free to direct me to any resources/articles/videos/etc that would be useful to help me learn more about this kind of stuff.
If you have a polygon (a mathematical one, what GeoJSON would call a linear ring), and a point, imagine a line from the point to the first coordinate of the polygon. As you move the end of your line to the second coordinate of the polygon, your line moves by some angle (might be positive, might be negative, might be zero). Similarly from the second to third and so on. Add up the angles: if the result is +/-360 degrees, your point is inside the polygon. If zero, then outside. Search "winding number" for details.
Of course, it would be inefficient to test all polygons, so you make some kind of index of the bounding boxes of the polygons, and only inspect those whose bounding box contains your point, these are called R-trees (there are many variants, and lots of software available for this).

Find closest city via GPS coordinates - algorithm

I am given coordinates for my location (55.1858, -162.7211). I would like to find the city I am at (or near) if it exists in my dataset. The dataset (tens of thousands of coordinates) has all cities that I am interested in, and one or more coordinates corresponding to that city (depending on the size of the city):
Cold Bay, Alaska, 55.1858,-162.7211
False Pass, Alaska,54.8542,-163.4113
King Cove, Alaska, 55.0628,-162.3056
...
What's the best algorithm (preferably in JavaScript) to find the city that I am in (Without using any APIs, Google Maps etc..)?
I had a few ideas, but they're probably not the best as they're all brute force methods:
Draw a radius around my coordinates of a certain distance and then loop through the dataset to find if any of the existing coordinates are in this radius. If one or more are, then loops through them and see which I am closest to via their distance.
Somehow, start to round my coordinates at the furthest decimal place and check after each rounding if this new set of rounded coordinates exists in the dataset.
I feel like these are really bad ideas and would love some guidance or recommendations on good algorithms for this type of searching.

Is there a way to determine if a given point (lat, long) is inside a drawn polygon?

I have a KML file with about 5 polygon placemarks and I was wondering if there's a way to determine if a specified point is inside any of the polygons. Does Google Earth have a function to do that or can one be written?
Use the ray casting method for point in polygon test.
The test shoots off a ray towards infinity (arbitrarily chosen direction, usually parallel to one of the axis used for code simplicity and speed) from the test point and counts the intersections with the polygon in question. If the number of intersections are odd, then the point lies within the polygon, else it lies outside. Repeat this test for all the polygons.
check out this link for further explanation and degenerate and special cases.
pip wikipedia .
I've implemented this function in C so let me know if you need any pointers.
You could alternatively also use the Winding Number test. WN wikipedia
They may have similar performances depending on the implementation and the platform.
The implementations you'l find may be mostly for Cartesian co-ordinates. Keep in mind that they'l work perfectly fine as it is, with geographic co-ordinates as well.

how to find latlong from distance?

I want to calculate the estimated location. Suppose i have source (A) and destination (B). Let say vehicle take 18 hours to reach from source to destination. After one hour it departs, the vehicle is at point C. At point C i have speed, Lat Long and distance cover from source (A). let say it cover 100km in 2 hours at point C. At this point i want to calculate the estimated location i.e where the vehicle will be after 2 hours or what will be the location after 300 km from point C with respect to current time,speed ,location and distance at point C. Vehicle is moving along the road. Please help me in this regard. Thanks
Not actual code but some possible hints to the answer. That's how I would do it.
I assume that you use the Google Direction API and that you know how to calculate a distance between two points from their coordinates.
From this, my idea would be call the API to get your route and to the use the polyline part of the answer. The polyline is encoded with this algorithm. You can use this javascript code to get your list of points.
You then calculate the distance between each per pair of points from the start. When you reach the distance from point A to point C, you know on which segment of the polyline your vehicle should be. To get the exact coordinates on the segment, I suggest you use the interpolate function of the Google Maps Geometry API.
Of course, if your route contains many segments, you might want to use a heuristic such as approximating the search of the middle segment by filtering your list by using a box of coordinates.

How does Google Maps and Nokia Maps generate routes from point to point

I will like to know if anyone has an idea on the concept behind point to point route generation on google maps and nokia maps. What logic was used to determine the route and generate directions from any point on the map to another? I wouldn't mind guesses or something of that sort. I just want to understand, how it works.
This is just a guess, but probably something like Dijkstra's algorithm. It most likely is some kind of graph-search algorithm, with each node representing an intersection and each edge representing a section of street.
I will also add that the graph here is likely also weighted, with each weight corresponding to how important the road is. For example, Interstate highways may have a greater weight than state highways, which have a greater weight than local roads, which have a greater weight than simple streets. Optionally, toll roads may have a lower weight than non-toll roads.

Categories

Resources