Google sometimes returns the incorrect U.S. state when reverse geocoding a lat/long. Presumably this is because Google is trying to return the nearest street address, which in some cases is not in the same state as the lat/long you are trying to reverse geocode.
Though it may not be a common scenario in practice, it's pretty easy to reproduce by playing around with a map: http://gmaps-samples.googlecode.com/svn/trunk/geocoder/reverse.html
For my application, I am less concerned about getting the nearest address and more concerned about always getting the correct U.S. state for a lat/long. Is there a way to achieve this with Google's API?
Thank you
Iterate over all results and pick the one with "administrative_area_level_1" in results[i].types
This is better than taking the "equivalent" address component from the first result, i.e. finding "administrative_area_level_1" in results[0].address_components[j].types
When reverse geocoding snaps your latlng to the nearest address which happens to be in a different state (or country), the state/country address component of the first result will be that of where that address is, but the subsequent result will be the state/country where the input latlng is.
Example: 42.834185,-0.302811 is in Spain, but snaps to an address in France.
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D42.834185%252C-0.302811
results[0].address_components[3].types = ["administrative_area_level_1", "political"]
results[0].address_components[3].short_name = "FR"
results[6].types = ["administrative_area_level_1", "political"]
results[6].short_name = "ES"
Related
I have been using Google's reverse geocoding APIs in a vehicle tracking application to convert lat/lon information into an "address" for at least 5 years. Recently, this conversion has started yielding some surprising results.
For example, the lat/lon pair, 36.7653111,-121.74852, when plugged into Google Maps, yields "CA-156, Castroville, CA 95012" as the address. This is the desirable answer.
The tracking application yields "11298 Haight St, Castroville, Monterey County, CA, 95012, US" The problem is that the JSON result contains two "street_address" and one "route" type. The dumb algorithm of choosing the first street_address or route occurring in the result no longer works. The question now is how to decide which of the possibilities is a better match to the given lan/lon? The lat/lon is clearly on route CA-156. Haight St. does not cross CA-156 at all.
What is special about this case is that the vehicle is not travelling on either of the streets in the two "street_address" types but is on the street in the route. In this case, the route should have been given priority over the two street_address types.
I have now examined the results of hundreds of reverse geocodings. There does not appear to be any simple algorithmic way of choosing the best result. For example, reverse geocoding 37.31674,-122.0472125 returns only two results:
Type: premise
Address: Child Development Center, Cupertino, Santa Clara County, CA 95014, US
location_type: ROOFTOP
37.316425,-122.0460558 Distance: 354.7286202778164 Feet
Type: route
Address: CA-85, Cupertino, Santa Clara County, CA 95014, US
location_type: GEOMETRIC_CENTER
37.3145586,-122.0461306 Distance: 855.4738140974437 Feet
The vehicle is travelling on CA-85. Choosing the first result (premise) or the result with least distance, does not yield the best result.
The fundamental problem here is the for "route" types, the distance to the GEOMETRIC_CENTER does not tell you if you are "on the route" (0 distance) or if you are "off the route", how far off.
I have filed a case with Google. If I get a useful response, I will post it here.
If you are reverse geocoding lat/lon information coming from in-vehicle devices here are two approached that significantly improve the results. The discussion assumes you have limited the results to types: "premise", "street_address" or "route", If you are interested in other types, you may have to experiment a bit.
First, if the in-vehicle device returns the speed along with lat/lon, then choose the "route" result, if one is present, when the speed is above a certain threshold. Otherwise, choose the "street_address" or "premise" with the least distance to the lat/lon. You may have to experiment a bit with the speed threshold to find a reasonable value. For me, 25 MPH seemed to do a decent job.
Second, if you don't have speed or another indication that the vehicle is stopped or moving, then try the following "hack".
Scan the results up to the first occurrence of a "route" and determine amongst "premise" or "street_address" types the one with the least distance to the lat/lon. Remember the "route", if one is found.
Then
1. If no "route" result exists, return the "premise" or "street_address" with the least distance to the lat/lon.
2. Else
a. If the "route" has a "route" name that matches the regex "[A-Z]+-[0-9]+", return the route as the best result.
b. Else if a least distance "premise" or "street_address" exists, return that as the best result.
c. Otherwise, return the "route" as a best result.
This is far from perfect, but seems to work well enough for the US which is all I care about right now. As route names differ significantly from country to country some enhancement will likely be necessary.
is there anyone using OpenWeatherMap api? , just a quick question, Im using their current condition API, but it doesnt return a wind directorion(deg) property on this lat lang (14.6760, 121.0437), it does when I input a different location, does this mean the wind is going NORTH "N if I dont receive a deg property from OpenWeatherMap?
Edit: I just noticed, its not just on that coordinates, If the wind is only at 1.5mph, it doesnt have a direction, does this mean its automatically north?
How can I simulate continuous traffic flow from historical data which consists of:
1. Vehicle ID;
2. Speed;
3. Coordinates
without knowing the routes of each vehicle ID.
This is a commonly asked questions but probably hasn't been answered here before. Unfortunately the answer largely depends on the quality of your input data mainly on the frequency / distance of your location updates (it would be also helpful if there is a time stamp to each datum) and how precise the locations fit your street network. In the best case there is a location update on each edge of the route in the street network and you can simply read off the route by mapping the location to the street. This mapping can be done using the python sumolib coming with sumo:
import sumolib
net = sumolib.net.readNet("myNet.net.xml")
route = []
radius = 1
for x, y in coordinates:
minDist, minEdge = min([(dist, edge) for edge, dist in net.getNeighboringEdges(x_coordinate, y_coordinate, radius)])
if len(route) == 0 or route[-1] != minEdge.getID():
route.append(minEdge.getID())
See also http://sumo.dlr.de/wiki/Tools/Sumolib#locate_nearby_edges_based_on_the_geo-coordinate for additional geo conversion.
This will fail when there is an edge in the route which did not get hit by a data point or if you have a mismatch (for instance matching an edge which goes in the "wrong" direction). In the former case you can easily repair the route using sumo's duarouter.
> duarouter -n myNet.net.xml -r myRoutesWithGaps.rou.xml -o myRepairedRoutes.rou.xml --repair
The latter case is considerably harder both to detect and to repair because it largely depends on your definition of a wrong edge. There are almost clear cases like hitting suddenly the opposite direction (which still can happen in real traffic) and a lot of small detours which are hard to decide and deserve a separate answer.
Since you are asking for continuous input you may also be interested in doing this live with TraCI and in this FAQ on constant input flow.
Here's my problem: a smartphone will send to my server some gps coordinates (latitude,longitude,altitude) and I'll have to compare these to an address stored in db in order to see how much distance there is between smartphone and address.
I'll need to obtain this address coordinates as well in order to do the actual comparison.
Is there a good and easy to use gps library for java?Any suggestions?
In your answers please note that I need a way to get coordinates from an address too!! So, given an address "second street 2,New York, zip code 01245", I need to find latitude,longitude,altitude,ecc.
Android's Location class has a static method distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results). You can look at the source code and use it in your program.
You could take a look at
A distance calculator using GeoCodes
Distance between 2 geocodes
I'm currently working on an app where the user inputs and address, which is then converted into coords. A database of locations is then queried and locations with in, say, 5km of search location is returned.
The problem I'm having is the accuracy returned by the geocodeAddressString function. When searching: Auckland, New Zealand, I'm getting back -36.90000, 174.70000, which is about 10 km's off the correct result. It's a few suburbs over.
Is there any way to improve on this? The Google Maps result is -36.848479, 174.763373, which you can see is much sharper and what I'm after.
Thanks!