I'm trying to get an address from a location. I'm doing so inside a asynctask inside a Fragement. All the other code works fine (checking an API and setting some UI stuff based on it) but this section to do the geocoding just won't. I followed an example from elsewhere on StackOverflow (can't remember the exact topic).
TextView locTxt = (TextView) findViewById(R.id.locationText);
Geocoder geocoder;
List<Address> addresses;
Double x = 55.971627;
Double y = -3.602585;
try
{
geocoder = new Geocoder(getActivity(), Locale.ENGLISH);
addresses = geocoder.getFromLocation(x, y, 1);
StringBuilder str = new StringBuilder();
if (geocoder.isPresent())
{
Toast.makeText(getApplicationContext(),
"geocoder present", Toast.LENGTH_SHORT).show();
Address returnAddress = addresses.get(0);
String localityString = returnAddress.getLocality();
String city = returnAddress.getCountryName();
String region_code = returnAddress.getCountryCode();
String zipcode = returnAddress.getPostalCode();
str.append(localityString + "");
str.append(city + "" + region_code + "");
str.append(zipcode + "");
locTxt.setText(str);
}
else
{
Toast.makeText(getApplicationContext(), "geocoder not present", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {}
When it gets to the if statement for geocoder.isPresent() is fails the if and just continues on with the rest of the program.
The issue is with Android Emulator. Even when you set coordinates via the command line the emulator will not preform GeoCoding code and therefore fails on my check if GeoCoder present. Works just fine on a physical device.
Related
i m using google Reverse Geocoding API in my app, i m succussfully able to get get coordinate using google geolocation API. Now i m trying to get Location Name from Reverse Geocoding API , but always returns Location not found error
here is my code:
Geocoder geocoder= new Geocoder(MainActivity.this, Locale.ENGLISH);
if(geocoder.isPresent()){
List<Address> list;
try {
list = geocoder.getFromLocation(37.42279, -122.08506,1);
Address address = list.get(0);
Log.d("this is working","thsi sis working");
StringBuffer str = new StringBuffer();
str.append("Name: " + address.getLocality() + "\n");
str.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
str.append("Admin Area: " + address.getAdminArea() + "\n");
str.append("Country: " + address.getCountryName() + "\n");
str.append("Country Code: " + address.getCountryCode() + "\n");;
String strAddress = str.toString();
JsonDatas.setText(strAddress);
Log.d("Address", strAddress);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i have several question
how google know that which app requested the API and how google determine of Quota that app. in google API developer Console, google gave Quota for app API
Why i m getting location not found error but searching on google map location is showing
do i need to add google Geocoder APi key into my app - right now i m only Geolocation API key using for retrieve Coordinates
Please correct me if i m wrong, Please give suggestion so my code will work fine
thanks
Make sure you have latest Google play library in your project. I am using this code and working for me.
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
Context mContext;
public ReverseGeocodingTask(Context context) {
super();
mContext = context;
}
// Finding address using reverse geocoding
#Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
String addressText = "";
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
Thread.sleep(500);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
addressText = String.format(
"%s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(1) : "", "", "");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return addressText;
}
#Override
protected void onPostExecute(String addressText) {
}
}
Where LatLng is Latitude and longitute, check this for doc.
and to use write down new ReverseGeocodingTask(this).execute(latlng); where you want to get data.
Make sure you are adding permission into your manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
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);
}
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();
}
}
how to convert the latitude and longitude in to postal address. i am not possible to use mapview to get geopoint.I wanted to convert latitude and longitude in address without use of mapview
This is called Reverse Geocoding, there are number of services that you can use including Google, Yahoo and Bing
See this article for free Geocoding services
Android has GeoCoder class for this :
public String getAddress(double latitude, double longitude){
if (Geocoder.isPresent()) {
try {
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
if(addresses != null && addresses.size() > 0) {
Address first = addresses.get(0);
StringBuilder sb = new StringBuilder();
if(first.getMaxAddressLineIndex() > 0){
sb.append(first.getAddressLine(0) + ", ");
}
sb.append(first.getLocality() + ", ");
sb.append(first.getCountryName());
return sb.toString();
} catch (Exception e) {
Log.e("MyAPP", "Reverse geo lookup failed", e);
return "Unavailable"
}
} else {
return "unavailable";
}
}
Go to project properties > Android > set project build target to Google
I wanna draw a route on the basis of the entered source & destination address. I need to get lat/log on the basis of address to draw a map in android. So how can i get lat/log on the basis of entered address.
Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName("Loc",1);
String add = "";
if (addresses.size() > 0)
{
add = addresses.get(0);
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
Use the Google Geocoding API to get coordinates for an address: http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding