0

Possible Duplicate:
ZipCode from location

I need to find zip code for current location. Is this possible?

Community
  • 1
  • 1
nirav
  • 411
  • 2
  • 8
  • 18

2 Answers2

3

You can do it with Google's Geocoding API. What you're actually looking for is reverse-geocoding, but it supports that too. You can have it return XML and then parse it for the postal code (a.k.a zipcode). Should be quite simple actually.

To use the API, simply access it by HTTP with the correct latitude and longitude:

https://maps.googleapis.com/maps/api/geocode/xml?latlng=37.775,-122.4183333&sensor=true

Then parse the XML using XPath, or your favorite XML parser (or just use JSON):

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//GeocodeResponse/result/address_component[type=\"postal_code\"]/long_name/text()";
InputSource inputSource = new InputSource("https://maps.googleapis.com/maps/api/geocode/xml?latlng=37.775,-122.4183333&sensor=true");
String zipcode = (String) xpath.evaluate(expression, inputSource, XPathConstants.STRING);

I haven't actually tested this bit of code (and it obviously needs exception handling), but I trust you'll be able to get it working from here.

As for getting the location itself, this previous question sums it up nicely:

How do I get the current GPS location programmatically in Android?

Community
  • 1
  • 1
kichik
  • 33,220
  • 7
  • 94
  • 114
  • I have tested it on emulator but it not working .How can i find Zip Code from city name and country name ?? – nirav Jul 09 '11 at 07:45
  • 1
    To use address instead of latlang, simply replace latlang with address in the URL. As for "not working", you'd have to be far more specific. At least an error message or something... – kichik Jul 09 '11 at 09:05
1

There is no "easy way" to get the actual Zip Code of the user. You could use the reverse geocoder function to do this but it doesn't always return the Zip Code, nor is it always accurate. Either way, you'll have to have location services and internet access active in order to do it.

OR

Please check this link.

Community
  • 1
  • 1
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • I have internet active in my device . Then how can i find Zip Code from user current location ?? – nirav Jul 09 '11 at 06:15