Android:逆ジオコーディング-getFromLocation
-
19-08-2019 - |
質問
long / latに基づいて住所を取得しようとしています。このようなものが動作するはずです?
Geocoder myLocation = Geocoder(Locale.getDefault());
List myList = myLocation.getFromLocation(latPoint,lngPoint,1);
問題は、取得し続けることです:メソッドgeocoder(Locale)は、savemaplocationタイプでは未定義です
支援があれば役立ちます。ありがとう。
ありがとうとにかく、
まだ取得中なので、機能しませんでした:メソッドGeocoder(Context、Locale)は、savemaplocationタイプでは未定義です
持っているもの:import android.location.Geocoder;
解決
次のコードスニペットは私のためにそれをやっています(latとlngはこのビットの上で宣言されたdoubleです):
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
他のヒント
これは、UIをブロックせずにジオコーダーの回答を取得するためのスレッドとハンドラーを使用した完全なサンプルコードです。
ジオコーダーコールプロシージャは、ヘルパークラスに配置できます
public static void getAddressFromLocation(
final Location location, final Context context, final Handler handler) {
Thread thread = new Thread() {
@Override public void run() {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> list = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
// sending back first address line and locality
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
Log.e(TAG, "Impossible to connect to Geocoder", e);
} finally {
Message msg = Message.obtain();
msg.setTarget(handler);
if (result != null) {
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("address", result);
msg.setData(bundle);
} else
msg.what = 0;
msg.sendToTarget();
}
}
};
thread.start();
}
UIアクティビティのこのGeocoderプロシージャの呼び出しは次のとおりです。
getAddressFromLocation(mLastKownLocation, this, new GeocoderHandler());
そして、UIに結果を表示するハンドラ:
private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
String result;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
result = bundle.getString("address");
break;
default:
result = null;
}
// replace by what you need to do
myLabel.setText(result);
}
}
Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
ここでは2つのことが起こっているようです。
1)コンストラクターを呼び出す前にnew
キーワードを見逃した。
2)Geocoderコンストラクターに渡すパラメーターが正しくありません。 Locale
が必要なContext
を渡します。
2つのGeocoder
コンストラクターがあり、どちらもINTERNET
を必要とし、1つはmanifest
を使用します:
Geocoder(Context context, Locale locale)
Geocoder(Context context)
ソリューション
有効なコンテキストを渡すようにコードを変更し、<=>をインクルードすれば、準備完了です。
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
注
まだ問題がある場合は、許可の問題である可能性があります。ジオコーディングは暗黙的にインターネットを使用して検索を実行するため、アプリケーションではマニフェストに<=> uses-permissionタグが必要です。
マニフェストの<=>ノード内に次のuses-permissionノードを追加します。
<uses-permission android:name="android.permission.INTERNET" />
この理由は、存在しないバックエンドサービスです:
Geocoderクラスには、コアAndroidフレームワークに含まれていないバックエンドサービスが必要です。プラットフォームにバックエンドサービスがない場合、Geocoderクエリメソッドは空のリストを返します。
最初に、LocationおよびLocationManagerクラスを使用して緯度と経度を取得します。次に、Get the city、address infoの以下のコードを試してください
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
都市情報がsbになりました。 sbを文字列に変換します(sb.toString()を使用)。
まあ、私はまだ困惑しています。それで、ここにもっとコードがあります。
マップを離れる前に、SaveLocation(myMapView,myMapController);
を呼び出します。これが、ジオコーディング情報を呼び出すことになります。
ただし、getFromLocation
はIOException
をスローできるため、SaveLocationを呼び出すには次の操作を行う必要がありました
try
{
SaveLocation(myMapView,myMapController);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
次に、IOExceptionsがスローされると言ってSaveLocationを変更する必要があります:
public void SaveLocation(MapView mv, MapController mc) throws IOException{
//I do this :
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
List myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
//...
}
そして毎回クラッシュします。
ジオコーダーはバグだらけだと聞きました。最近、Googleのhttpジオコーディングサービスを使用して緯度/経度から場所を検索するサンプルアプリケーションをまとめました。こちらで自由にチェックしてください
これを使用
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);