Geocoder not working ICS but works in Gingerbeard - android

Well i have made a application that uses Google Maps.
I am currently using Tap on Map to get the address through Geocoder but it is not working.
I have tried the code in Gingerbeard e.g HTC chacha, Samsung Galaxy S2(Gingerbeard) it works well and get the address but fails to get the address in ICS
Yes i have used Google API 14, have uses all permissions, also used declare the google maps library in Manifest.And also have the proper keystore keys
I tried the Google Sample Code Examples
e.g: http://developer.android.com/training/basics/location/currentlocation.html
HTC DESIRE X, ICS:
HTC CHACHA Screen shot:
how should i solve this?

GeoPoint point = new GeoPoint(
(int) (LATITUDE * 1E6),
(int) (LONGITUDE * 1E6));
String n;
public void someRandomMethod() {
n = convertPointToLocation(point);
}
public String convertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}

Related

Android Reverse Geocoding

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.

android google map get latitude and longtitude issue

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

android reverse geocoding service not found exception on emulator 2.3.3

I am Currently using android emulator 2.3.3 . i want to get the location from coordinates. but i am getting this "service not available" exception.
here is my code:
#Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
if (e.getAction() == 1) {
GeoPoint point = mapView.getProjection().fromPixels(
(int) e.getX(), (int) e.getY());
Geocoder coder = new Geocoder(GoogleMaps.this, Locale.ENGLISH);
try {
List<Address> addresses = coder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
String address = "";
Address a = addresses.get(0);
Toast.makeText(GoogleMaps.this, a.getCountryName(), 300).show();
} catch (IOException e1) {
Toast.makeText(GoogleMaps.this, e1.getMessage(), Toast.LENGTH_LONG).show();
}
}
return false;
I had the same issue some time ago, and I discovered that indeed there is such a problem for the emulators with Android 2.3.3. I could find a better solution than to use another emulator with different Android version.

Reverse address using Geocoder

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

android - Geocoder.getFromLocationName() is not working in ICS device

I have two devices. One is HTC WildFire S and other one is HTC 1V. I used the Geocoder.getFromLocationName() in my application. It is running successfully in the HTC wildfire S. But in the HTC 1V i got the following error. why it's came? How can i solve this? please can anybody help me.
Code
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
//s is the address
List<Address> addresses = geoCoder.getFromLocationName(s, 5); //Here i got the following Exception.
Error
06-18 16:28:17.933: W/System.err(4960): java.io.IOException: Service not Available
06-18 16:28:17.953: W/System.err(4960):at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
Location Tab
Finally i found the answer :https://code.google.com/p/android/issues/detail?id=38009
Reboot your device for Geocoder to work. Hope it helps someone
Note: some say it will work if you use Google API 16
GeoPoint point = new GeoPoint(
(int) (LATITUDE * 1E6),
(int) (LONGITUDE * 1E6));
String n;
public void someRandomMethod() {
n = convertPointToLocation(point);
}
public String convertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}

Categories

Resources