基于城市、邮编或街道名称获取纬度和经度

3

在我的当前的安卓应用中,我想根据输入的城市名、街道名或邮政编码获取地理坐标。我该如何实现呢?

最好的问候, Rony

4个回答

6

请查看方法Geocoder.getFromLocationName(cityName, maxResults)

使用方法如下:

List<Address> addressList = geoCoder.getFromLocationName(cityName, 1);
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
    double selectedLat = address.getLatitude();
    double selectedLng = address.getLongitude();
}

5

您好,请尝试以下代码以从给定地址获取地理编码点。

List<Address> foundGeocode = null;
/* find the addresses  by using getFromLocationName() method with the given address*/
foundGeocode = new Geocoder(this).getFromLocationName("address here", 1);
 foundGeocode.get(0).getLatitude(); //getting latitude
 foundGeocode.get(0).getLongitude();//getting longitude

1
你好, 有一个非常不错的网站叫做世界地名录,它提供了你所需的数据,且可以下载(按城市名称)。 http://www.world-gazetteer.com/home.htm 在主页上,点击链接: 其他统计数据......各种统计数据:表格、地图和可下载数据 并从弹出的页面中,选择如下链接: popdata(1.4 MB) 解压缩文件,你就得到了所需的数据库! 该数据库是包含世界各国城市的免费主数据库,其中包括纬度、经度和人口数据等。 此信息来源为:http://answers.google.com/answers/threadview?id=432778

从其他答案来看,我认为Geocoder可能比下载地点和坐标数据库更好的解决方案。 - Kev

0

试试这个。

Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
    List<Address> addresses = geoCoder.getFromLocation(latitude , longitude, 1);

    String strCompleteAddress= "";
    //if (addresses.size() > 0)
    //{
        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
            strCompleteAddress+= addresses.get(0).getAddressLine(i) + "\n";
    // }
    Log.i("MyLocTAG => ", strCompleteAddress);
    Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();
}
catch (IOException e) {
    Log.i("MyLocTAG => ", "this is the exception part");
    e.printStackTrace();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接