Is there a way to determine the State in which a given Lat/Long resides using the Google Api in Android?
thx
As quoo mentionned, you can use reverse geocoding. From quoo's link,
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
You can get then the state by using the getAdminArea() method from the Address object.
myList.get(0).getAdminArea()
See more info here of what information you can get from the Address object
I assume you could use reverse geocoding to figure out the state. There was a similar question for android asked here:
Android: Reverse geocoding - getFromLocation
Related
I have used Google geo address and using lat/long have got the address. How do I determine if I am on road on not? Basically hit-test for road? I am currently using location manager for lat/long and the GeoAddress is same for on road and next to road.
Final Solution
Use reverse geocoding via the Google Geocoding API.
The term geocoding generally refers to translating a human-readable
address into a location on a map. The process of doing the converse,
translating a location on the map into a human-readable address, is
known as reverse geocoding.
The Geocoding API supports reverse geocoding directly using the latlng
parameter. For example, the following query contains the
latitude/longitude value for a location in Brooklyn:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false
The docs explain how to interpret the json results but StackOverflow has various answers addressing more efficient ways to parse the data.
https://stackoverflow.com/a/8095967/1085891
https://stackoverflow.com/a/8314094/1085891
https://stackoverflow.com/a/6335080/1085891
Possible Solutions
Getting the nearest street/road coordinates in android - the answer to this question may point you in the right direction.
You can use directions api directly: http://maps.googleapis.com/maps/api/directions/json?origin=51,19&destination=51,19&sensor=false
Could you use the API to find directions which would then provide the closest road?
Stick position to road on android maps v2
GPSLocator - App to Find Current (Nearest) Location using GPS - possibly helpful; not so sure on this one.
Snap to Roads Android - Exact same question
Quick Search Results
Snap to nearest street
Maps API Blog using GDirections.loadFromWaypoints
Finding nearest street given a lat-long location
Driving route from my location to destination in Google Maps Android API V2
Unsolved Similar Questions
Trace the location only on Road in Google maps V2 android
https://stackoverflow.com/questions/18081417/google-maps-v2-snap-to-road-issue
See fiddle.
It can be done so much easier with OSRM (nearest) webservice. Make a GET request like:
const url = '//router.project-osrm.org/nearest/v1/driving/';
fetch(url + coord.lon + ',' + coord.lat).then(function(response) {
return response.json();
}).then(function(json) {
if (json.code === 'Ok') {
console.info(json.waypoints[0].location);
}
});
Which is a road/street coordinate.
References — Project-OSRM
Since I own an app key for mapquest (community edition), all geocoding requests end up in US only. Before I entered the app key geocoding european locations was possible.
I couldn't find a method to set a default country. Even initializing the geocoder object with a Locale didn't work.
Any suggestions?
Currently the mapquest Android API doesn't support single line international geocoding.
This is the reply I got from mapquest:
I checked at our Android API is still using our v1 geocoder which does
not support single line international geocoding. My support suggested
you use our geocoding web service for the geocoding first:
http://developer.mapquest.com/web/products/dev-services/geocoding-ws
Again just make sure the request says “v2”.
I've created an autocomplete address search text box. My problem is that I just can't make it work fine:
Addresses are out of the bounding box usually, whatever I do - this didn't help.
Finds addresses that doesn't contain the typed text.
Finds less addresses, than max but it doesn't contain the good result.
The result list is totally irrelevant sometimes.
I need to type almost the whole address to get the correct result.
Source:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
if (Geocoder.isPresent()) {
// Bounding box: Hungary
addresses = geocoder.getFromLocationName(text, 5, 46.13, 21.96, 48.89, 16.69);
}
} catch (IOException e) {
Log.v(tag, "Geocoding error: " + e.getMessage());
e.printStackTrace();
}
My aim is to make an address search text box which gets the same result as in Android Maps seach text box - except the previously typed/favourite addresses and etc., which is an other story.
The Google Places Autocomplete API may be of help. As far as I know you must license it for use in commercial apps. If your app is for free then you should be fine.
EDIT about inaccurate Geocoder results:
I haven't yet played around much with Geocoder. But what I can tell you so far is the following:
You need to take into consideration that on some devices all methods return null (check Geocoder.isPresent() to be sure) because Geocoder relies on some underlying implementation that can be missing on some devices.
And it is also possible that these results come from an other Geocoder engine / service than Google Places.
On top of that the query results are never perfect. You rarely get the exact address of the current location either because the location is off or because the Geocoder data is not accurate enough (or simply outdated).
If you really need an accurate address you should give the user both a list of best candidate options and some means for manually editing the address.
If you need more accurate results based on the location name you could take a look at available online services that perform better for your needs. One example is geonames.org. If their database is good enough for you, then you need to get or implement some Java API for interfacing with the service of your choice and use that instead Geocoder.
I have the following code in my Android program
List<Address> addressList = geoCoder.getFromLocation(37.790551,-122.433931, 1);
if (!addressList.isEmpty()) {
address = addressList.get(0);
String number = address.getSubThoroughfare();
String streetName = address.getThoroughfare();
.....
}
This is a request to essentially get a street name (hence the 1) from a latitude and longitude in San Francisco.
When I execute this program on a phone in Europe (specifically Ireland) number is returned as 2250-2290 and streetname is returned as Fillmore St.
When I get a friend to run the same code on his phone in California number is null and streetname is still Fillmore st.
I've seen other SO questions allude to region settings but is this possible that the geographical location can affect the query in the Android API?
The Javascript Geocoding API mentions region bias. I wonder is it possible to do something similar in the Android API. I did try creating the Geocoder as
Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.US);
but this does not re-recreate the California based result.
From getting people to run identical code on different continents it seems that location is indeed a factor.
The Javascript geocoding allows you to specify a region though
Are the phones running different versions of android? Also, another thing to keep in mind is that "The Geocoder class requires a backend service that is not included in the core android framework." So, if the two phones are calling upon different backend services to query for the Geocoder, that may explain the output you are seeing.
I think with Android 1.5, they included a Google API backend service implementation for Geocoder. I don't know if newer versions of Android and the Google API made changes to the Geocoder implementation that may be giving you different results or it's coming from some other backend service.
I want to search for GPS locations for with Google Maps. I have already registered with Google Maps API, and got the key. I can successfully pinpoint my current location on a map. The next part is to search for items around the current GPS location.
Approach 1: I tried using Android's geocoder.getFromLocationName("UPS",5) but I am not getting anything.
Approach 2: hit Google https://maps.googleapis.cm/maps/apo/place/search but it needs a client id. To get a client id I have to create a premier account. Do I have to do all this?
Any suggestion how to use maps to search location for Android?
"GPS" is the place you're looking up? Not a city or street name? I think that's the problem here.
Maybe look at:
How can I find the latitude and longitude from address?
You will want to use a better "address" other than UPS to find locations using Geocoder. Do you realize how many UPS locations will be found. getFromLocationName() is best used with detailed location info and a locale directive so it limits its searching to the Locale you specify. Also, you will frequently get IOExceptions in the emulator. Lastly using SDK 2.3 and above will not work with Geocoder.
Try something like this:
Geocoder geocoder = new Geocoder(this, Locale.US);
String staddress = "Georgia Tech, Atlanta, GA";
try{
List<Address> loc = geocoder.getFromLocationName(staddress, 5);
}
catch(IOException e) {
Log.e("IOException", e.getMessage());
Toast.makeText(this, "IOException: " + e.getMessage(),20).show();
Notice how the locale is set to US when I new up my Geocoder. Also notice the try catch around the call to geocoder. That will catch any IO Exceptions and allow you ot handle them as I did using a Toast message and some other flow control statements that are out of sight