Reverse address using Geocoder - android

I am trying to get an address for a specific geopoint but when i try it using try and catch it fails.Here it is my code
srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6));
Geocoder geocoder=new Geocoder(getBaseContext(), Locale.getDefault());
try
{
List<Address> addresses=geocoder.getFromLocation(srcGeoPoint.getLatitudeE6()/1E6,
srcGeoPoint.getLongitudeE6()/1E6,1);
String add="";
if(addresses.size()>0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
tv.setText(add);
}
catch(IOException e)
{
e.printStackTrace();
}
It fails on this line
List addresses=geocoder.getFromLocation(srcGeoPoint.getLatitudeE6()/1E6,
srcGeoPoint.getLongitudeE6()/1E6,1);
any help??

The geocoder does not work in the emulator as described more here:
Does geocoder.getFromLocationName not work on emulators?
I also implemented myself and it really does not in the emulator but does on a device.

I don't have the power to approve answers yet but Diego is 100% correct, geocoder does not work on the emulators you have to use a real device if you want to debug it. Trust me, it took the longest time banging my head up against a wall until I found out what was wrong. So do yourself a favor so you dont get a concussion, just use a real device

You would cast these to doubles:
srcGeoPoint.getLatitudeE6()/1E6
srcGeoPoint.getLongitudeE6()/1E6
full code:
List addresses=geocoder.getFromLocation((double) (srcGeoPoint.getLatitudeE6()/1E6), (double) (srcGeoPoint.getLongitudeE6()/1E6),1);

Related

Geocode returns null address in lollipop

I have a problem in getting the location address from latitude and longitude as I am using the geocoder for getting the address and it is working fine in the MarshMallow and above 5 devices but not working in the version 5 (lollipop) and i want to run my code in the version 5 also. Any help is appreciated and thanks in advance
here is my code
if (this.geocoder == null) {
this.geocoder = new Geocoder(this, Locale.getDefault());
}
try {
List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lng), 1);
if (!addresses.isEmpty()) {
address = addresses.get(0).getLocality();
PrefsManager.with(this).save(Constants.PREF_ADDRESS,addresses.get(0).getLocality());
return addresses.get(0).getLocality();
}
} catch (IOException e) {
e.printStackTrace();
}
I have tested your code with dummy lat and lng in android version 5.1. it is working fine.
It is my suggestion for you please debug your code with break points. check is it the issue with geocode or with something else. Please check may be you are getting location for MarshMallow version in permission check block not getting in else part which works for lolipop and other lower versions.

Geocoder does not work for certain addresses

I am using the goecoder to try to convert address to lat/lng. However, it seems the geocoder works for some addresses but not others. If I search the address on google maps then it pinpoint it perfectly.
Here is an example 38 Crichton Street, Ottawa, ON, Canada
This address returns 0 results when I run the code below
Location loc = null;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocationName(addr, 1);
if (address.size() == 0) {
return null;
}
double latitude = address.get(0).getLatitude();
double longitude = address.get(0).getLongitude();
loc = new Location("Unknown");
loc.setLatitude(latitude);
loc.setLongitude(longitude);
} catch (IOException ioException) {
Log.e(TAG, ioException.toString());
} catch (IllegalArgumentException illegalArgumentException) {
Log.e(TAG, illegalArgumentException.toString());
}
Any idea why or how I can fix it
Thank you
Not sure why native Android API geocoder cannot find this address. I can see that this address is found in Geocoding API web service:
https://maps.googleapis.com/maps/api/geocode/json?address=38%20Crichton%20Street%2C%20Ottawa%2C%20ON%2C%20Canada&key=MY_API_KEY
You can also see it in Geocoder tool:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D38%2520Crichton%2520Street%252C%2520Ottawa%252C%2520ON%252C%2520Canada
As a workaround you can consider using the Geocoding API web service. Please note that there is a Java client library for web services that you can find on Github:
https://github.com/googlemaps/google-maps-services-java
The Javadoc for client library is located at
https://googlemaps.github.io/google-maps-services-java/v0.2.5/javadoc/
I hope this helps!

Geocoder not working ICS but works in Gingerbeard

Well i have made a application that uses Google Maps.
I am currently using Tap on Map to get the address through Geocoder but it is not working.
I have tried the code in Gingerbeard e.g HTC chacha, Samsung Galaxy S2(Gingerbeard) it works well and get the address but fails to get the address in ICS
Yes i have used Google API 14, have uses all permissions, also used declare the google maps library in Manifest.And also have the proper keystore keys
I tried the Google Sample Code Examples
e.g: http://developer.android.com/training/basics/location/currentlocation.html
HTC DESIRE X, ICS:
HTC CHACHA Screen shot:
how should i solve this?
GeoPoint point = new GeoPoint(
(int) (LATITUDE * 1E6),
(int) (LONGITUDE * 1E6));
String n;
public void someRandomMethod() {
n = convertPointToLocation(point);
}
public String convertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}

android - Geocoder.getFromLocationName() is not working in ICS device

I have two devices. One is HTC WildFire S and other one is HTC 1V. I used the Geocoder.getFromLocationName() in my application. It is running successfully in the HTC wildfire S. But in the HTC 1V i got the following error. why it's came? How can i solve this? please can anybody help me.
Code
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
//s is the address
List<Address> addresses = geoCoder.getFromLocationName(s, 5); //Here i got the following Exception.
Error
06-18 16:28:17.933: W/System.err(4960): java.io.IOException: Service not Available
06-18 16:28:17.953: W/System.err(4960):at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
Location Tab
Finally i found the answer :https://code.google.com/p/android/issues/detail?id=38009
Reboot your device for Geocoder to work. Hope it helps someone
Note: some say it will work if you use Google API 16
GeoPoint point = new GeoPoint(
(int) (LATITUDE * 1E6),
(int) (LONGITUDE * 1E6));
String n;
public void someRandomMethod() {
n = convertPointToLocation(point);
}
public String convertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}

android problem to get latitude & longitude from a given address

I am trying find lat & long from user given address, for that i am using following code,
GeoPoint pointForEnterAddr;
try {
List<Address> address = coder.getFromLocationName(enterAddr, 1);
if (address == null)
pointForEnterAddr = null;
else {
Address location = address.get(0);
System.out.println("Lat in button click :"
+ location.getLatitude());
System.out.println("Long in button click :"
+ location.getLongitude());
pointForEnterAddr = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
}
catch (Exception ex) {
System.out.println("Error here :" + ex);
}
But everytime it coming to catch part & shows exception,
java.io.IOException: Service not Available
How can i solve it and get proper output? Plz tell me.
Thank you
I found this for your questions, I think it is the bug of emulator. Look at this answer
Android; Geocoder, why do I get "the service is not available"?
Just to mention an alternative, you can use the Google Maps' REST based reverse geocoder. You can probably use this as a fallback. As you would be using Google Maps in your app too, you should be good with the API rules.

Categories

Resources