android google map get latitude and longtitude issue - android

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

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!

Android Geocoder returns null city name

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.

Service not available when I try to get address from Geocoder in android?

Sometimes I get the IOException when I try to get address through latitude and longitude via the geocoder class of android.
This occurs rarely and force closes my application.
private void locAdd(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(ReportActivity.this,
Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
Log.e("Addresses", "-->" + addresses);
} catch (IOException e) {
e.printStackTrace();
}
}
This is the code I use to get location. Is there any issue with the code?
How do I solve it?
Logcat
java.io.IOException: Unable to parse response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:136)
at com.pstpl.crimeverify.CrimeReportActivity.locAdd(CrimeReportActivity.java:554)
at com.pstpl.crimeverify.CrimeReportActivity.access$10(CrimeReportActivity.java:549)
The API documentation,
http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation%28double,%20double,%20int%29
says that IOException will occur "if the network is unavailable or any other I/O problem occurs".
I don't think there is any problem with the code, since it works for "sometimes".
BTW, you are passing latitude1 and longitude as method arguments, but you are using latitude and longitude.

How to get city name ontap on the map in google map android v2?

By tapping on the map, How can I get the address of that location. On tap at particular location we can able listen and pass those co-ordinates we can get address but it should make work in android Google map v2.
If you have your LatLng data - it's quite easy. You need to use Goecoder.
protected String doInBackground(Location... params) {
Geocoder geocoder =
new Geocoder(mContext, Locale.getDefault());
// Get the current location from the input parameter list
Location loc = params[0];
// Create a list to contain the result address
List<Address> addresses = null;
try {
/*
* Return 1 address.
*/
addresses = geocoder.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
} catch (IOException e1) {
Log.e("LocationSampleActivity",
"IO Exception in getFromLocation()");
e1.printStackTrace();
return ("IO Exception trying to get address");
} catch (IllegalArgumentException e2) {
// Error message to post in the log
String errorString = "Illegal arguments " +
Double.toString(loc.getLatitude()) +
" , " +
Double.toString(loc.getLongitude()) +
" passed to address service";
Log.e("LocationSampleActivity", errorString);
e2.printStackTrace();
return errorString;
}
// If the reverse geocode returned an address
if (addresses != null && addresses.size() > 0) {
// Get the first address
Address address = addresses.get(0);
/*
* Format the first line of address (if available),
* city, and country name.
*/
String addressText = String.format(
"%s, %s, %s",
// If there's a street address, add it
address.getMaxAddressLineIndex() > 0 ?
address.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
// Return the text
return addressText;
} else {
return "No address found";
}
}
...
}
...
}
The best way to do it is to use AsyncTask. You will find complete example at Android Devlopers' site: http://developer.android.com/training/location/display-address.html
This may help you to use the complete map opertion
You can do this with the android GeoCoder. With GoogleMaps v2 you can use longpress to get a LatLng point. By taking those coordinates and passing them to the GeoCoder you can "reverse geocode" to find the address.

get latitude and longitude using zipcode

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.

Categories

Resources