Geocoder city fetch issue - android

I am using google's geocoder class to fetch city with given lat-long.
I am facing this problem in India where earlier city name was Banglore and now it is changed to bengaluru.
So when I fetch using geocoder.getFromLocation method I get Banglore instead of bengaluru
While using the same lat-long I call Geocoder api directly, then I get the right city value i.e bengaluru.
Following is code geocoder class code
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String city = null;
if (geocoder != null) {
if (geocoder.isPresent()) {
try {
List<Address> addressList = geocoder.getFromLocation(params[0], params[1], 1);
city = addressList.get(0).getLocality();
} catch (Exception e) {
e.printStackTrace();
}
}
}
My target and compile sdk version is 23.
Thank you in advance.

Related

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!

Convert coordinates to String

In my app i have got a lot of coordinates like this:
String uri ="uber://action=setPickup&pickup=my_location&dropoff[latitude]=
59.936435&dropoff[longitude]=30.296409&dropoff[nickname]=Place1";
How i can update them with Parse.com? Maybe i need to make String with this coordinates, that can be updated with Parse.com?
Also i have got coordinates google maps:
mMap.addMarker(new MarkerOptions().position(new LatLng(59.624895, 30.230149)).title("Palce3"));
How i can update them?
You need a Geocoder object. Try this:
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(your_latitude, your_longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
addresses.get(0).getAddressLine(0);
Notice that Internet connection is required for Geocoder to work.

How to get continuous Location (Lat and Lng) along with location address in same project?

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

Can't get the Address from Geocoder

I have created simple code just for getting address from string location. Code brakes at starred line. Why ?? if I print the exception : Service not Available
List<Address> address;
String myAddress = "Vilnius";
Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault());
System.out.println("coder : :" + coder);
**address = coder.getFromLocationName(myAddress, 1);**
ObjLoc1adr = coder.getFromLocationName(ObjLoc1String, 1);
ObjLoc2adr = coder.getFromLocationName(ObjLoc2String, 1);
Because of this you should always wrap that code in a try block to catch an IOException. There is no guarantee that the Geocoder always find a location or the service is reachable. This hasn't to be a fault of your code or device. A simple connection problem or timeout is enough to brake your App.
To be save have something like this:
if(GeoCoder.isPresent()) {
Geocoder geocoder = new Geocoder(this);
String myAddress = "Vilnius";
List<Address> addresses = null;
try {
address = geocoder.getFromLocationName(myAddress, 1);
if (!addresses.isEmpty()) {
Address address = list.get(0);
// do something with your address
} else {
// No results for your location
}
} catch (IOException e) {
e.printStackTrace();
}
}

Unable to use geocoder, gives unhandled type IOException

This is the code
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = **gcd.getFromLocation(latitude, longitude, 1);**
The portion that gives the error is highlighted with asterisks.
Thank you.
getFromLocation() throws an IOException if the network is unavailable or any other I/O problem occurs.
http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation(double, double, int)
To solve, surround it with a try/catch block:
try {
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
}
catch (IOException e) {
e.printStackTrace();
}
In addition to Ken WOlfs answer, you have to check if the GeoCoder Service is available on the device.
"The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists." - Google
So your code should look like this:
if(Geocoder.isPresent()){
Geocoder gcd = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
} catch (IOException e) { e.printStackTrace(); }
}

Categories

Resources