来自位置/地点的Android locationoint

Yog*_*esh 4 android google-maps geocoding geolocation

我已经看过许多从Geocoder获取位置名称然后通过getFromLocation()获取地址的示例,但是如何通过名称获取geoPoint作为位置.用户输入城市名称,然后将其转换为地理位置并在地图上显示.在Android或谷歌API中有没有办法.

我不是指当前的位置,而是指任何位置给它的名字.基本上我希望它允许用户获取远程城市的天气更新.我可以通过获取当前位置来获取当前位置.

Lon*_*ian 8

试试这个,你最好在一个单独的线程而不是UI线程中运行它.你既可以得到AddressGeoPoint用这种方法.

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;
}
Run Code Online (Sandbox Code Playgroud)