Android geopoint from location/locality

android, geocoding, geolocation, google-maps

Solution

Try this, you'd better run it in a separately thread rather than UI-thread. You can both get `Address` and `GeoPoint` by this method.

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;
}

Problem

I have seen numerous examples of getting location name from Geocoder and then get addresses by getFromLocation() but how can I get geoPoint for a location by name. User enters a city name and I turn them into a geopoint and show on map. Is there a way in Android or google API's. I dont mean current location but any location by giving its name. Basically I want it to allow user to get weather update for remote cities.I am able to do so for current location by getting current locality.

Original source