C/C++プログラマの管理者が, Androidプログラムにチャレンジ. AndroidプログラミングのTipsをメモっていく予定です.

地図の位置情報から住所を取得するには, Geocoderクラスを利用する.

位置情報から住所を取得

Googleマップを表示する」を参照し, Googleマップが表示できるようにする.

  • MainActivity.java
    • MapView#getMapCenterメソッドで, 地図の中心位置情報を取得する.
    • GeoPoint#getLatitudeE6/getLongitudeE6メソッドで, 緯度/経度を取得する.
    • Geocoderのインスタンスを生成する.
    • Geocoder#getFromLocationメソッドで, 住所情報のリストを取得する.
    • Address#getMaxAddressLineIndex/getAddressLineメソッドで, 各住所情報を取得する.
package com.moonlight_aska.android.web.googlemap01;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MainActivity extends MapActivity {
  private MapView mview;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mview = (MapView)findViewById(R.id.mapview);
  }

  @Override
  protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
  }

  public void onGetAddress(View view) {
    // 中心位置の緯度/経度取得
    GeoPoint gpo = mview.getMapCenter();
    double latitude = gpo.getLatitudeE6() / 1E6;
    double longitude = gpo.getLongitudeE6() / 1E6;
    // 住所の取得
    StringBuffer strAddr = new StringBuffer();
    Geocoder gcoder = new Geocoder(this, Locale.getDefault());
    try {
      List<Address> lstAddrs = gcoder.getFromLocation(latitude, longitude, 1);
      for (Address addr : lstAddrs) {
        int idx = addr.getMaxAddressLineIndex();
        for (int i = 1; i <= idx; i++) {
          strAddr.append(addr.getAddressLine(i));
          Log.v("addr", addr.getAddressLine(i));
        }
      }
      Toast.makeText(this, strAddr.toString(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
  • res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity" >

  <com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="****************************************"  // Maps APIキー
    />
  <Button android:id="@+id/btnAddr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/getAddr"
    android:onClick="onGetAddress"
    />
</RelativeLayout>
  • 動作例
SHARP SH-03C / Android 2.2




このページへのコメント

.getAddressLine(i)で1以上を指定するとnullが返る場合があるようなのでforループでは0から始めた方がいいと思います(0にまとめて住所が入っています)。Addressクラスの仕様が変わった可能性もありますが。

0
Posted by 流 2019年04月22日(月) 07:33:38 返信

askaです.

> GeoPointやMapActivityなどを抜くと
> エラーになってしまいます。

「位置情報から住所を取得する」に記載のコードから, 手っ取り早く
指定位置の住所の取得及び地図の非表示を行う方法をお知らせした
だけですので, GeoPointやMapActivityを抜くと当然エラーとなります.

> googleのAPIキーは必要なのでしょうか。

Google MAPのAPIではなく, Android自体のAPIなのでMAPs API Keyは
不要のはずです.

0
Posted by aska 2016年01月14日(木) 23:37:19 返信

GeoPointやMapActivityなどを抜くと
エラーになってしまいます。

googleの地図は表示させなくても可能らしいのですが、googleのAPIキーは必要なのでしょうか。

0
Posted by android初心者 2016年01月13日(水) 10:40:55 返信

askaです.

以下で可能かと思いますので, 試してみてください.
1) 位置座標(緯度, 経度)を, Geocoder#getFromLocationメソッドに渡すことで
住所が取得できます.
2) 地図は, MapView#setVisibilityで非表示にできます.

0
Posted by aska 2016年01月12日(火) 23:56:55 返信

地図表示をせず、位置座標から住所だけを表示させるにはどうしたら良いでしょうか。

0
Posted by android初心者 2016年01月12日(火) 10:11:42 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu


逆引き(基礎編)

画面表示/操作(49)

フラグメント(1)

逆引き(応用編)

Firebase(2)

AD



管理人/副管理人のみ編集できます