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.
Related
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.
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?
I created sample application I want to get latitude and longtitude from string address line. this is my method which I used to get latitude and longtitude
private void getFromLocation(String address) {
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(address, 1);
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
latitude = p.getLatitudeE6() / 1E6;
longtitude = p.getLongitudeE6() / 1E6;
HAMBURG = new LatLng(latitude, longtitude);
Log.d("latitide", "" + latitude);
Log.d("longtitude", "" + longtitude);
} else {
Log.d("Addess", "Address not found");
}
} catch (Exception ee) {
Log.d("ex", ee.toString());
}
}
This method throws an exception " java.io.IOException: Unable to parse response from server
". Is there any mistake I made? is there any other solution to get latitude and longtitude ?
An IOException is likely to indicate that the remote request failed for some relatively low-level reason;
IOException: if the network is unavailable or any other I/O problem occurs
public List<Address> getFromLocationName (String locationName, int maxResults)
Added in API level 1
Returns an array of Addresses that are known to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", etc.. The returned addresses will be localized for the locale provided to this class's constructor.
The query will block and returned values will be obtained by means of a network lookup. The results are a best guess and are not guaranteed to be meaningful or correct. It may be useful to call this method from a thread separate from your primary UI thread.
Parameters
locationName: a user-supplied description of a location
maxResults: max number of results to return. Smaller numbers (1 to 5) are recommended
Returns:
a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
Throws
IllegalArgumentException: if locationName is null
IOException: if the network is unavailable or any other I/O problem occurs
So, Do
Please try to avoid several hits for address in a minute.
Run this code on different device.
for more detail check this artical.
List<Address> foundGeocode = null;
/* find the addresses by using getFromLocationName() method with the given address*/
try {
foundGeocode = new Geocoder(MainActivity.this).getFromLocationName("Your location",1);
Double s=foundGeocode.get(0).getLatitude(); //getting latitude
Double n=foundGeocode.get(0).getLongitude();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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);
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.