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.
Related
I want to get the current location for android with out using Google APIs Key that's why
I'm Using the Geocoder.
It is working absolutely for many devices but it not for some selected devices like Latest android version_12 and in OnePlus_v10,11,12.
if (Geocoder.isPresent())
{
try
{
Geocoder geocoder = new Geocoder(contex, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
{
cityName = addresses.get(0).getAddressLine(0);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
Please provide the solution for the same share with someone you know who can fix it.
Also is there any changes in new android device regarding GPS,WI-FI, Network Checker.
I have an application that must collect address from a marker on Google Maps API. I tested this code and it works fine on devices that run Android 6.0 :
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addressList1 = geocoder.getFromLocation(currentLatitude,currentLongitude,1);
Address addressCurrent = addressList1.get(0);
full_address = addressCurrent.getAddressLine(0); //Detail address
country = addressCurrent.getCountryName();
//locality = addressCurrent.getLocality(); //sub-district
admin_area = addressCurrent.getAdminArea(); //City
sub_admin_area = addressCurrent.getSubAdminArea(); //District
road_name = addressCurrent.getThoroughfare(); // road
Toast.makeText(this, full_address, Toast.LENGTH_SHORT).show();
}catch (IOException e){
Toast.makeText(this, "No Data", Toast.LENGTH_SHORT).show();
Log.e("error geocoder", e.toString());
}
But when I try to run on Android 8.1 devices it returns an error log:
E/errorĀ geocoder: java.io.IOException: NetCode:-1000,NetRes:{}
I tried to search for it but found no solution.
How can I fix this?
As Gencoder doc say: Use the isPresent() method to determine whether a Geocoder implementation exists.
so call isPresent() method firstly, and manually check device network status.
If still failed, maybe you need create custom Geocoder implementation, this link is helpful.
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!
Sometimes I get the IOException when I try to get address through latitude and longitude via the geocoder class of android.
This occurs rarely and force closes my application.
private void locAdd(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(ReportActivity.this,
Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
Log.e("Addresses", "-->" + addresses);
} catch (IOException e) {
e.printStackTrace();
}
}
This is the code I use to get location. Is there any issue with the code?
How do I solve it?
Logcat
java.io.IOException: Unable to parse response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:136)
at com.pstpl.crimeverify.CrimeReportActivity.locAdd(CrimeReportActivity.java:554)
at com.pstpl.crimeverify.CrimeReportActivity.access$10(CrimeReportActivity.java:549)
The API documentation,
http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation%28double,%20double,%20int%29
says that IOException will occur "if the network is unavailable or any other I/O problem occurs".
I don't think there is any problem with the code, since it works for "sometimes".
BTW, you are passing latitude1 and longitude as method arguments, but you are using latitude and longitude.
The code below works perfectly for real devices running on 1.5, 1.6
and 2.0 as well as the emulator running on 2.1.
However, executing it
on the Nexus One (running 2.1) raises an IOException:
java.io.IOException: Unable to parse response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:124)
That's the code snippet where it happens:
Double myLatitude = AppObject.myLocation.getLatitude();
Double myLongitude = AppObject.myLocation.getLongitude();
DEBUG.i(TAG, "My location: " + myLatitude + " | " + myLongitude);
Geocoder geocoder = new Geocoder(MainActivity.this);
java.util.List<Address> addressList;
try {
addressList = geocoder.getFromLocation(myLatitude, myLongitude, 5);
if(addressList!=null && addressList.size()>0) {
currentAddress = new String();
DEBUG.i(TAG,addressList.get(0).toString());
currentAddress = addressList.get(0).getAddressLine(0) + ", "
+ addressList.get(0).getAddressLine(1) + ", "
+ addressList.get(0).getAddressLine(2);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
Hi I fell into the same problem and same situation. I got a Nexus One and I got a workaround to make addressList = geocoder.getFromLocation(myLatitude, myLongitude, 1); work.
You just have to restart your device. What I noticed is that Geocoder's getFromLocation method isn't aware of any change in Locale settings (in my case, what made the method stop working was the change of my default Locale).
After a restart everything worked. Anyone got a reasonably reason for this? (Sorry for the word game :-P )
I'm using a HTC Tmobile G2, and I had the same problem while testing a GeoCoder based feature. A reboot helped, but that's not an acceptable solution when my customers start complaining about this. If this is some kind of cache clearing issue, then I hope a workaround can be put in place programmatically.