- Android
- 2014-08-27
GoogleMap APIを利用して、住所から座標を取得します。
String search_key = "座標を取得したい住所";
ジオコーダーオブジェクトのインスタンスを生成
Geocoder gcoder = new Geocoder(this, Locale.getDefault());
結果を返してほしい件数を指定します。
int maxResults = 1;
結果を代入する変数を宣言
List<Address> lstAddr;
実際に取得してみましょう。
// 位置情報の取得 lstAddr = gcoder.getFromLocationName(search_key, maxResults); if (lstAddr != null && lstAddr.size() > 0) { // 緯度・経度取得 Address addr = lstAddr.get(0); double latitude = addr.getLatitude(); double longitude = addr.getLongitude(); Toast.makeText(this, "位置\n緯度:" + latitude + "\n経度:" + longitude, Toast.LENGTH_LONG).show(); }
例外処理は省いていますが、IllegalArgumentExceptionとIOExceptionが発生する可能性があります。
続きを読む…»