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.
Related
I try get city according to current location with specific language i do that before like below and all work good but in my new project its not work!
Geocoder geo = new Geocoder(getApplicationContext(), new Locale("ja"));
List<Address> addresses = null;
try {
addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0) {
Log.d("CITY",addresses.get(0).getLocality());
city.setText(addresses.get(0).getLocality());
} else {
// do your stuff
}
} catch (IOException e) {
e.printStackTrace();
}
dos not matter which language be set always name of city return with English language!
after some research i understand must set geo.getFromLocation maxResults > 1 cause always first address returned in list , return with English language but the next items will be your specific language , so just need change
addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
to
addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 2);
and getLocality like this addresses.get(1).getLocality()
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.
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!
I want to show a continuous update of Lat and long along with location address .
I was able to receive continuous lat/lng but wasn't able to find the location address .
Basically , want to combine these two :
https://developer.android.com/training/location/receive-location-updates.html
https://developer.android.com/training/location/display-address.html
How can I achieve this?
Thanks.
Hi you can get a location address using Geocoder class
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
String city;
try {
addresses = gcd.getFromLocation(lat,lon, 1);
if (addresses.size() > 0)
city = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
When I go maps|settings|edit home or work I see my home & work address, cool!
Anybody know how to get that same information from the Android SDK? I am working on a maps app and would like to plot those to points of reference, with out asking the user for them again?
Thanks
You will need to use reverse geocoding to convert the latitude/longitude into an address. More info about geocoding can be found here.
Here is an example:
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> list = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
// sending back first address line and locality
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
Log.e(TAG, "Impossible to connect to Geocoder", e);
}