如何使用地址和邮政编码Android获得Lat Long



几年前有一个问题,如何仅使用地址获取经度坐标(这里是问题的链接:如何从地址中找到纬度和经度?

我理解那里公认的答案,但我的问题是,例如,在德国,您没有唯一的地址,因此如果我仅使用地址来获取纬度和长坐标,我可能会得到错误的纬度坐标。比如,有一个地址叫做"Hauptstrasse",在柏林和法兰克福。所以我会得到错误的坐标。

有没有办法使用地址和邮政编码来获得正确的经度坐标?

例如,此代码仅使用地址:

public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}

我遇到了同样的问题。

这就是我解决这个问题的方式。

我有一个包含地址、邮政编码和城市名称的 txt 文件。

以德国为例:德国Hauptstrasse 123,10978(这将在柏林的某个地方(

然后,我将这个 txt 文件放入 assets 文件夹中,并使用BufferedReader创建了一个数组。

public void readFile(){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("YOURFILE")));
String line;
while ((line = reader.readLine()) != null) {
String splittedLine [] = line.split(",");
int numberofElements =2;
String[] onlyAdressandZip = Arrays.copyOf(splittedLine, numberofElements);
} catch (IOException e) {
e.printStackTrace();
}

如您所见,我没有使用国家名称(德国(,这就是为什么我使用了Arrays.copyOf

Hauptstrasse 123, 10978 , 德国 有 3 的长度,但我们只需要 Hauptstrasse 123 和 10978 来解释int numberofElements =2

就是这样,然后您可以提供onlyAdressandZip[0]onlyAdressandZip[1]作为地理编码器的输入,以获得正确的经度和经度坐标。

最新更新