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;
}
Related
I am an entry level programmer and i want to create an app, in which an address converts to a specific latitude and longtitude. Is there an easy way to do this? I read a few things about geocoder. Could you please describe to me, where and how?
This can easily be done with GeoCoder. Here is the code for rescue ;)
public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}
where strAddress is the actual address you want long and lat.
Happy Coding :)
it throw IOException if the network is unavailable or any other I/O problem occurs. It need internet connection to use Geocoder.
public class MainActivity extends Activity {
double LATITUDE = 37.42233;
double LONGITUDE = -122.083;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
TextView myAddress = (TextView)findViewById(R.id.myaddress);
myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
e.printStackTrace();
myAddress.setText("Cannot get Address!");//and tell me what is
}
}
}
anyone has any idea?
I have managed to do it with Emulator 2.1 api 8, but then the reverse geocoding always give an empty result. anyone could confirm my code?
log cat
06-03 10:59:24.689: W/System.err(4129):java.io.IOException: Service not Available
:at android.location.Geocoder.getFromLocation(Geocoder.java:136)
:at com.example.gprsonandoff.MainActivity.city(MainActivity.java:74)
:at com.example.gprsonandoff.MainActivity.onCreate(MainActivity.java:44)
:android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
:at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
It seems to be a prob faced by many. Check this.
You can try using GeoCoding APIs if using Geocoder doesn't work out even if internet connection is there.
I Reboot My Android Device and Error is gone.
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;
}
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.
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);