Android Geocoder returns null city name - android

I am new to Android development, following is my code about use Geocoder to get city name of current location, it returns null:
private void updateCurrentLocation(Location location) {
double lat = 0.0, lng = 0.0;
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Log.i("tag", "Latitute is" + lat + ", Longtitute is" + lng);
} else {
City_Name = "Unavailable";
}
List<Address> list = null;
Geocoder geocoder = new Geocoder(this.getActivity());
try {
list = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
e.printStackTrace();
}
//may provide multiple locations.
if (list != null && list.size() > 0) {
Address address = list.get(0);
City_Name = address.getLocality();
}
Log.i("Try", "CityName:" + City_Name);
//send empty message
handler.sendEmptyMessage(1);
}
I opened GPS services, add ACCESS_FINE_LOCATION and INTERNET permission in Manifest already. Also, I searched similar questions in Stackoverflow about Geocoder returns null, but haven't found useful solutions. One of them is analyze JSON from Geocoder website, but it doesn't work either.
Can anyone help with this? Thank you!
BTW, is there any better solution to receive a city name? Thank you!

If the "getFromLocation" method gives you an empty set then it's because the server that is being looked up doesn't have the address information for the coordinates you're passing it. This is also noted in the docs. So I think that you should let it go and use another service like the Google Maps geocoding service or another one like Nominatim from the OpenStreetMap project.

Related

Get user's location on background service using FusedLocationProviderApi & Google Places API

My current code use FusedLocationProviderApi to get user's current city/locality. This code is in my Service :
try {
Geocoder gcd = new Geocoder(LocationUpdateService.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0)
{
txtStation.setText("Locality : " + addresses.get(0).getLocality() + " Sublocal : " + addresses.get(0).getSubLocality());
}
}catch (Exception e) {
Toast.makeText(LocationUpdateService.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
However i need to show user's more specific place (bus station, gas station, etc) and i think i must use Places API.
How to integrate the Places API to my current code (which use FusedLocationProviderApi)?
What i think now is to use FusedLocationProviderApi to get the basic latitude & longitude, and then use Places API with those coordinates.
Please kindly help me/show me some clue
Thanks a lot for your help
How about the PlaceDetectionApi?

How to get the home/work address from android maps 2 sdk

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);
}

android application which takes city name and return latitude and longitude

An android application which takes city name as input and return longitude and latitude of the city.
Which method i should used to do it?
What changes will be needed in android manifest file?
You can try using Forward GeoCoding like this:
if(Geocoder.isPresent()){
try {
String location = "MyLocation";
Geocoder gcoder = new Geocoder(this);
List<Address> addresses= gcoder.getFromLocationName(location, 5); // get the found Address Objects
List<LatLng> ll = new ArrayList<LatLng>(addresses.size()); // A list to save the coordinates if they are available
for(Address addr : addresses){
if(addr.hasLatitude() && addr.hasLongitude()){
ll.add(new LatLng(addr.getLatitude(), addr.getLongitude();
}
}
} catch (IOException e) {
// handle the exception
}
}
You will require permission for Internet in manifest file.
<uses-permission android:name="android.permission.INTERNET"/>
Here is a blog that can help you.
Hope this helps.

Geocoder getFromLocation(lat,lon) doesn't work

I have an activity which first uses an AsyncTask to get and parse data from a server.
The entries contain a latitude and a longitude.
Then, in onPostExecute() I load my adapter and display my listView. And in my adapter's getView() I try to display the location with the latitude and the longitude :
private String getPosition(String longitude, String latitude)
{
float lon = Float.parseFloat(longitude),
lat = Float.parseFloat(latitude);
Geocoder geocoder = new Geocoder(MyActivity.this, Locale.getDefault());
List<Address> addresses = null;
try
{
addresses = geocoder.getFromLocation(lat, lon, 1);
} catch (IOException e)
{
e.printStackTrace();
}
if(addresses == null || addresses.size() == 0) return "No location found.";
else
{
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
return (city + ", " + country.toUpperCase());
}
}
I always get "No location found." but I'm sure some entries are correct. For example one has a latitude of 39.017 and a longitude of 125.73. Google's API show there is at least one address for it :
http://maps.googleapis.com/maps/api/geocode/json?latlng=39.017,125.73&sensor=true
I've also declared this in my Manifest :
<uses-permission android:name="android.permission.INTERNET" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
But all this won't return any address. Any idea why?
geoCoder.getFromLocation (Reverse Geocoding) method will work when you build avd (virtual device) target as Google API 19 and also the application is built with the target set as "Google API 19" (you can modify the build target using Project->Properties). This worked for me. Should you need more details please reply to this post.

get latitude and longitude using zipcode

i want to get the latitude and longitude using the zipcode for android application
This is a much simpler solution, assuming the geocoder services are present:
final Geocoder geocoder = new Geocoder(this);
final String zip = "90210";
try {
List<Address> addresses = geocoder.getFromLocationName(zipCode, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
// Use the address as needed
String message = String.format("Latitude: %f, Longitude: %f",
address.getLatitude(), address.getLongitude());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} else {
// Display appropriate message when Geocoder services are not available
Toast.makeToast(this, "Unable to geocode zipcode", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// handle exception
}
There is no rival of Google in GeoCoding :)
You have to just make an http Get request for the following link.
Here's a link
Just change the postal code Element According to your requirements. And Enjoy.
For further reference follow the link.Maps Geocoding Reference
You could use YQL like so: select centroid from geo.places where text="ZIPCODE_HERE"
You will have to use a Geocoding Service, like the ones provided by Google or the freely available Nominatim. Note that since you will be just providing a ZIP code, you might not get the results you want due to the inaccuracy of the address itself.

Categories

Resources