Android geopoint from location/locality

送分小仙女□ 提交于 2020-01-02 06:34:10

问题


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.


回答1:


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



回答2:


use the below url to get lat and long from the address through reverse geo coding::

http://maps.googleapis.com/maps/api/geocode/json?address=hyderabad&sensor=false

or

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



回答3:


to get Lat and Long co-ordinates from address is geocoding not reverse geocoding. Simple get address convert it using geocode and, once you got the co-ordinates show location on map.



来源:https://stackoverflow.com/questions/10167819/android-geopoint-from-location-locality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!