如何从Android的当前位置获得ZIP code当前位置、Android、code、ZIP

由网友(独来独往)分享简介:可能重复: 从位置 邮编code Possible Duplicate:ZipCode from location 我需要找到拉链code的当前位置。这可能吗?I need to find zip code for current location. Is this possible?推荐答案您可以使用的谷...

可能重复:   从位置 邮编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:

How我得到当前GPS位置编程的机器人?

阅读全文

相关推荐

最新文章