如何从Android的当前位置获得ZIP code [英] How to get ZIP Code from current location in android

查看:130
本文介绍了如何从Android的当前位置获得ZIP code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  从位置 邮编code

Possible Duplicate:
ZipCode from location

我需要找到拉链code的当前位置。这可能吗?

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

推荐答案

您可以使用的谷歌的地理编码API 。你实际上寻找的是反向地理编码,但它支持这一点。你可以把它返回的XML,然后解析它的邮寄code(又名压缩code)。实际上应该是很简单的。

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.

要使用该API,只需访问HTTP通过使用正确的纬度和经度:

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

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

然后使用XPath,或者你喜欢的XML解析器解析XML(或只是使用JSON):

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

我还没有实际测试过的code此位(和它显然需要异常处理),但我相信你就可以得到它从这里工作。

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.

至于获得位置本身,这previous问题概括起来很好:

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

<一个href="http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android">How我得到当前GPS位置编程的机器人?

这篇关于如何从Android的当前位置获得ZIP code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆