本文介绍了Android版Places API:从Place.getAddress()获取地址行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用中,我使用Geocoder.getFromLocation()查找位置的地址.我可以得到一个Address对象,并使用Address.getAddressLine()得到一个地址的地址线.

In my android app, I find the address of locations using Geocoder.getFromLocation(). I can get an Address object and get the address lines of an address with Address.getAddressLine().

我只对第一个地址行感兴趣,可以通过Address.getAddressLine(0)轻松获得.

I am only interested in the first address line, which I can get easily with Address.getAddressLine(0).

此外,应用程序允许用户使用PlaceAutocomplete选择位置.当用户选择位置时,我会收到一个Place对象,并且可以通过Place.getAddress()获取地址.

Also, app allows user to choose locations using PlaceAutocomplete. When user chooses a location, I receive a Place object and I can get the address by Place.getAddress().

问题是,Place.getAddress()返回的是字符串而不是Address对象.该字符串包含完整地址,因此我无法像使用Address对象一样轻松获得第一条地址行.

The problem is, Place.getAddress() returns a string instead of an Address object. This string contains the full address, so I cannot easily get the first address line, as I did with Address object.

我应该如何从Place.getAddress()返回的字符串中找到第一个地址行?

How should I find the first address line from string returned by Place.getAddress()?

推荐答案

简短答案您无法从该API获取结构化数据.出于某种原因,Google决定人们在Android上不希望这样做.

Short answer is that you can't get structured data from that API. For some reason, Google has decided that is not something people would want on Android.

我的一位同事已将此问题记录在Google上,以请求向iOS和JavaScript用户提供相同级别的信息:

A colleague of mine has logged this issue with Google to request the same level of information as is provided to iOS and JavaScript users:

更长的答案是您的选择是:

  1. 您找到了解析该字符串的巧妙方法.不幸的是,不同区域之间实际的街道/郊区/城镇/省结构不一致(例如,南非的开普敦与南非的约翰内斯堡的句子结构不同).因此,您的解析规则必须非常聪明.
  2. 您使用其他Google API. JavaScript API为相关调用提供结构化数据. 此相关问题显示了有关该问题的更多详细信息API.不幸的是,Google建议您不要使用此技术.
  1. You figure out a clever way to parse that string. Unfortunately the actual street/suburb/town/province structure is not consistent across different areas (e.g. Cape Town in South Africa has a different sentence structure to Johannesburg in South Africa). So your parsing rules need to be very clever.
  2. You use a different Google API. The JavaScript API provides structured data for the related call. This related question shows more details on that API. Unfortunately Google recommends against using this technique.


我相信Google只是为了给我们提供这句话是为了挑选一个地址,而不是让我们获取有关该地址的结构化信息.为了获得更好的信息,您需要通过查询地点ID来获取经度/纬度值.


I believe that Google only intends to give us this sentence for the purposes of picking an address, rather than for us to get structured information about the address. In order to get better information, you need to get the lat/lng values by querying the place ID.

然后从这些经度/纬度值中进行反向地理定位以获得正确的地址.

From those lat/lng values, you can then reverse-geolocate to get the correct address.

不幸的是,在这项技术中,Google的API再次使我们失败.

Unfortunately in this technique, Google's APIs fail us once more.

  • 通常您可以将地址A解析为给定的经度/纬度,然后再解析为另一个地址B,该地址可能与A接近,但与A接近. API调用不像人们期望的那样可交换

我对这些电话有相当多的经验,希望我能提供另一个答案.如果您希望坚持使用Google的位置API,那么您将无法满足您的要求.

I have had a fair amount of experience with these calls, and I wish there was another answer I could provide. If you wish to stick with Google's location APIs, then you can't get what you are asking for.

这篇关于Android版Places API:从Place.getAddress()获取地址行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:07