I've got a page that has a map with a starting and ending location. I run a route between them to get the nifty line showing the route. I'm currently using Bing but have attempted with Google as well. I'd like to know which states this route passes through so I can then overlay those states with specific information.
Any suggestions on how to obtain this would be most appreciated.
I'm using the AJAX SDK's for both Bing and Google. Handling all the local stuff with js/jquery.
You can use a reverse geo-code request on the Google Maps API to determine what state a particular point is in. So I imagine you could process your array of points returned with the directions request and pull out the state for each one.
In the v2 API, this would be:
results.AddressDetails.AdministrativeArea.AdministrativeAreaName
I think this is a bit more intuitive in v3. You can examine the AddressComponents array in the results to find the appropriate type:
{
"long_name":"California",
"short_name":"CA",
"types":["administrative_area_level_1","political"]
}
You could optimize the reverse geocoding by using divide and conquer on the array of positions on the route (if the state is the same for the first and middle position, then don't do reverse geocoding on the intervening points).
The MapQuest Directions web service has a stateBoundaryDisplay flag that will explicitly put state boundary crossings (ie "Crossing into statename") in the narrative. You could easily pull just that info from the json/xml response with a text search of the narrative steps.
http://www.mapquestapi.com/directions/#advancedoptions
Hope that helps.
Roman
You could create your own service utilizing a shapefile and a library like SharpMap or a geodatabase like mysql spatial, sql server spatial, etc. Then you simply just need to run an intersection query to discover which states your route runs through. This approach would work for any polygon set, so you could easily extend the solution to counties, voting district, school districts, etc.
Related
I'm trying to see if there's a way in a single API call to find the ideal route, order not mattering, between X destinations.
For example, the program has 3 destinations, Jeff's house, Amy's house, and Valerie's house. Don't really care the order we go in, but we'd like to visit each house with the least amount of driving.
Right now, I have it set up such that we try every ordering of destinations, and settle on the one with the fastest time. But having so many API calls seems inefficient, but I can't see a way in the API to do what I want. Is what I want presently possible in the google maps API?
You can use Waypoints in Directions API web service which returns a route that includes pass throughs or stopovers at intermediate locations.
By default, the Directions service calculates a route through the
provided waypoints in their given order. Optionally, you may pass
optimize:true as the first argument within the waypoints parameter to
allow the Directions service to optimize the provided route by
rearranging the waypoints in a more efficient order.
Sample request:
https://maps.googleapis.com/maps/api/directions/json?
origin=Adelaide,SA&destination=Adelaide,SA
&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA
&key=YOUR_API_KEY
Note that requests using waypoint optimization are billed at a higher rate.
If you will be using client-side Maps JavaScript Directions Service. Refer to this documentation and example.
Hope this helps!
Trying to avoid reinventing the wheel here.
Anyone knows of any API that lets one create a route that starts and ends at the user's position, given as parameter the distance required for the route?
For example, you want to take a walk. A walk of a total of 5KM from home and back to home. Any API that will work with Google Maps to offer routes?
Thanks, as always.
You can implement this with the use of Google Maps API Directions Service which receives direction requests and returns computed results.
Furthermore, with this API, you can create a route that starts and ends at the user's position by Using Waypoints in Routes .
As mentioned,
Travel time is the primary factor which is optimized, but other factors such as distance, number of turns and many more may be taken into account when deciding which route is the most efficient.
Please note, however, that the use of Directions service must be in accordance with the policies described for the Google Maps Directions API.
I am now starting new project and I wanted to know if I could use google maps to solve my problem and get list of countries in selected route.
I tried to search on internet about this but couln't find anything what I needed.
Edit: So I created function which loops through routes steps and by given latitude/longitude coordinates in each step I can get in which country is this step taken by using googles geocoder.geocode function. But now I face new problem while I am looping through steps I get this error: OVER_QUERY_LIMIT indicates the webpage has sent too many requests within the allowed time period.
I even tried to set timeout for 500ms before caling geocode function but no use.
If you do a route request, it will always have details about crossing international boundaries.
Just try doing a from/to request on maps.google.com and you will see in the directions, when it crosses a border.
That same information is available to us to take from the direction response.
Google Maps API has any way to get the street coordinates of an location?
Is simple: I just want to get the nearest street coordinate. To got this i need, for example, all coordinates that compose a street.
Are there something like this?
You may use the directionService.
Pass the given address(or location) as origin and destination to directionsService.route() and use the travelMode DRIVING . The response should contain the nearest street.
Demo: http://jsfiddle.net/doktormolle/W3VGN/
I found this (ReverseGeocoding in v3): https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding
Since this thread is old I suppose this could be useful.
These answers are all old, and Google has rearranged the maps API significantly since the answers.
In 2018, the best way to turn a location (long, lat) into a "point on road," is to use the snap-to-road or nearest-road service:
https://developers.google.com/maps/documentation/roads/snap
Note that this API charges a cent per API call, and can take up to 100 distinct points per call. If latency and complexity aren't problems, if you need to answer this question on a client, you could build a server that collects up to 100 requests from different clients, makes one request to Google, and then returns the request data back to the appropriate clients. (For this use, make sure to use nearest-road, not snap-to-road.)
Also, currently, Google Maps has a $200 per-month statement credit available, which may make smaller uses of this API not actually end up costing much (or anything at all.)
It is easy to use the Google Maps API to find a specific street address and return the latitude and longitude. For example, link.
However, it appears that typing in the name of a specific location, for example a park, causes problems. Often these don't have a specific street number (at least, not easily findable). Despite the fact that Cadigal Reserve is located at the same address as in the link above, if I enter that as part of the query string and remove the street number, the results become rather useless: link
Typing this directly into maps.google.com easily finds the park itself (and of course, you could then find the latitude/longitude by looking in the URL).
Is there not any way of using the Google Maps API to geocode a park location like this?
It is important to understand that geocoding is not an exact science. The recommended practice if you have addresses that you know should geocode to a specific location is to build a cache and use local (client-side) geocoding.
In version 2 of the api you would build your own client-side cache that contains pre-computed geocoder responses by extending the GeocodeCache. Once a cache is defined, you would call the setCache() method and away you go. This is pretty much explained here:
http://code.google.com/apis/maps/documentation/javascript/v2/services.html#Geocoding_Caching
However, AFAIK GeocodeCache was removed in V3 of the api...
So, I would suggest implementing your own client -side caching-strategy of known addresses and their corresponding coordinates. When your application receives a geocode request for a known address the response would come from your cache (rather than Google's geocoding servers).
Failing all that you can always use a payed geocoding service that, in theory, will have a much more accurate dataset (as well as a higher limit on requests, etc).
Finally, you should also take a look through the Geocoding Strategies document as it gives a good handle on some of the issues here.