これらのGeocoder
メソッドとネットワークまたはi/o 呼び出しは、NetworkOnMainThreadException
. なので、迷ったら別スレでどうぞ!
GeoCoder.getFromLocation()
別のスレッドからメソッドを呼び出す方法の例を次に示します。
new AsyncTask<GeoPoint, Void, Address>()
{
@Override
protected Address doInBackground(GeoPoint... geoPoints)
{
try
{
Geocoder geoCoder = new Geocoder(context);
double latitude = geoPoints[0].getLatitudeE6() / 1E6;
double longitude = geoPoints[0].getLongitudeE6() / 1E6;
List<Address> addresses = geoCoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
return addresses.get(0);
}
catch (IOException ex)
{
// log exception or do whatever you want to do with it!
}
return null;
}
@Override
protected void onPostExecute(Address address)
{
// do whatever you want/need to do with the address found
// remember to check first that it's not null
}
}.execute(myGeoPoint);