【问题标题】:How to get Lat Long using Adress AND Zip Code Android如何使用地址和邮政编码 Android 获取经纬度
【发布时间】:2017-08-12 15:00:07
【问题描述】:

几年前有一个问题,如何仅使用地址获取经纬度坐标(这里是问题的链接:How can I find the latitude and longitude from address?

我理解那里接受的答案,但我的问题是,例如,在德国你没有唯一的地址,所以如果我只使用地址来获取经纬度坐标,我可能会得到错误的经纬度坐标。就像,在柏林和法兰克福有一个地址叫做“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;
    }
}

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    我遇到了同样的问题。

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

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

    以德国为例:Hauptstrasse 123, 10978, Germany(这将在柏林某处)

    然后,我将这个 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] 作为GeoCoder 的输入,以获得正确的经纬度坐标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      相关资源
      最近更新 更多