Androidプログラマへの道 〜 Moonlight 明日香 〜 - 住所から位置情報を取得する
住所から地図の位置情報を取得するには, Geocoderクラスを利用する.

住所から位置情報を取得

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

  • MainActivity.java
    • Geocoderのインスタンスを生成する.
    • Geocoder#getFromLocationNameメソッドで, 住所から位置情報のリストを取得する.
    • Adress#getLatitude/getLongitudeで, 緯度/経度を取得する.
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.view.View;
import android.widget.EditText;
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 onGetLocation(View view) {
    Geocoder gcoder = new Geocoder(this, Locale.getDefault());
    List<Address> lstAddr;
    EditText name = (EditText)findViewById(R.id.editAddr);
    try {
      // 位置情報の取得
      lstAddr = gcoder.getFromLocationName(name.getText().toString(), 1);
      if (lstAddr != null && lstAddr.size() > 0) {
        // 緯度/経度取得
        Address addr = lstAddr.get(0);
        int latitude = (int)(addr.getLatitude() * 1E6);
        int longitude = (int)(addr.getLongitude() * 1E6);
        Toast.makeText(this, "位置\n緯度:" + latitude + "\n経度:" + longitude, Toast.LENGTH_LONG).show();
        // 地図表示
        GeoPoint gpo = new GeoPoint(latitude, longitude);
        mview.getController().animateTo(gpo);
      }
    } 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キー
    />
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <EditText android:id="@+id/editAddr"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:layout_weight="5"
      android:inputType="text"
      />
    <Button android:id="@+id/btnLocate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/getLocate"
      android:onClick="onGetLocation"
      />
  </LinearLayout>
</RelativeLayout>
  • 動作例
SHARP SH-03C / Android 2.2