get latitude and longitude using zipcode - android

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.

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.

How to get the home/work address from android maps 2 sdk

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

How widespread is Geocoder implementation on Android mobile devices?

I need to fetch longitude and latitude by street name. I saw that Geocoder can do that job. The documentation tells us that "the Geocoder class requires a backend service that is not included in the core android framework". That means that I can't be sure that Geocoder is implemented in Android devices.
My question is, how widespread is the Geocoder implementation. If there are only very few devices without Geocoder, I can live with that, but if there are a lot of devices, I have to rethink this functionality of my app.
We should use isPresent() method of GeoCoder class to decide so.
http://developer.android.com/reference/android/location/Geocoder.html#isPresent()
A GeoCoder client is included along with Google Maps, so any device with Google Maps should have a working GeoCoder implementation. By and large, Android devices have Google Maps installed. Other than that, there could be no Google Maps, and some other GeoCoder backend available. In such a case, you can use the following code to determine if the device has a working GeoCoder implementation:
final Geocoder geocoder = new Geocoder(this);
final String locName = "1600 Amphitheatre Parkway, Mountain View, CA";
try {
final List<Address> list = geocoder.getFromLocationName(locName, 1);
if ( ! (list == null || list.isEmpty()) ) {
final Address address = list.get(0);
System.out.println("Geocoder backend present, (lat,lon) = (" + address.getLatitude() + ", " + address.getLongitude() + ")");
}
else {
System.out.println("Geocoder backend not present");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Get weather update of current location?

What I have: Latitude and Longitude.
What I want: Get weather update for these coordinates.
If you want to use the Google Weather API, you'll have to pass it either a City, State or a Zip code. To do this, you'll need to GeoCode your lat/long to get this info.
Here's the URL to the Google Weather API: http://www.google.com/ig/api?weather=Seattle,WA
Here's a sample code to take lat/long and convert to zip:
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 3);
} catch (IOException ex) {
//Handle IOException
}
for (int i = 0; i < addresses.size(); i++) {
Address address = addresses.get(i);
if (address.getPostalCode() != null)
String zipCode = address.getPostalCode();
}
Then pass the Zip Code (Or City, State) to the Google Weather API and parse the returning XML. Good luck!
google API is down, so you should look for an alternative.
http://openweathermap.org/api
You need to use an API. You'll have to do some research on your own, here's one good one.
http://www.weather.gov/xml/

Categories

Resources