从位置/地区的Andr​​oid的GeoPoint [英] Android geopoint from location/locality

查看:94
本文介绍了从位置/地区的Andr​​oid的GeoPoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了地理codeR越来越地名的许多例子,然后通过getFromLocation获得地址(),但我怎么能得到的GeoPoint按名称的位置。用户进入一个城市的名字,我把它们变成一个GeoPoint对象,并在地图上显示。有没有在Android或谷歌API的一种方式。

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.

我不意味着当前位置,但给出它的名字的任何位置。基本上我希望它允许用户获取天气更新远程cities.I我能够通过获得当地电流为当前位置这么做。

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.

推荐答案

试试这个,你最好运行在单独线程而不是UI线程。你可以都得到地址的GeoPoint 用这种方法。

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

这篇关于从位置/地区的Andr​​oid的GeoPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆