Android中从位置/地点获取geopoint

4
我看过很多从Geocoder获取位置名称并通过getFromLocation()获取地址的示例,但如何通过名称获取位置的geoPoint呢?用户输入城市名称,我将其转换为geopoint并在地图上显示。在Android或Google API中是否有方法?
我不是指当前位置,而是通过给出名称来获取任何位置。基本上,我想让用户获取远程城市的天气更新。通过获取当前地区的方式,我已经能够做到这一点。
3个回答

8

尝试这个方法,最好在单独的线程中运行它而不是UI线程。你可以通过这种方法同时获取地址地理坐标

public static Address searchLocationByName(Context context, String locationName){
    Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
    GeoPoint gp = null;
    Address ad = null;
    try {
        List<Address> addresses = geoCoder.getFromLocationName(locationName, 1);
        for(Address address : addresses){
            gp = new GeoPoint((int)(address.getLatitude() * 1E6), (int)(address.getLongitude() * 1E6));
            address.getAddressLine(1);
            ad = address;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ad;
}

4
请使用以下URL通过反向地理编码从地址获取纬度和经度:
http://maps.googleapis.com/maps/api/geocode/json?address=hyderabad&sensor=false

或者

http://maps.googleapis.com/maps/api/geocode/xml?address=hyderabad&sensor=true

-1

从地址获取纬度和经度坐标是地理编码而不是反向地理编码。 简单地获取地址,使用地理编码进行转换,一旦获得了坐标,就在地图上显示位置。


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