2011年6月24日金曜日

GoogleMap-住所や地名から位置情報を調べる

1.MapActivity クラス

public class MapActivity extends MapActivity {
 private MapView mapview;
 private Button gomap;
 private EditText address;
 private List<Address>addressList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapview= (MapView)findViewById(R.id.theMap);
        mapview.getController().setZoom(15);
        mapview.setBuiltInZoomControls(true);
        gomap=(Button)findViewById(R.id.buttonAdress);
        address=(EditText)findViewById(R.id.editAdress);
        gomap.setOnClickListener(new clickGomap ());
    }
 @Override
 protected boolean isRouteDisplayed() {
  // TODO 自動生成されたメソッド・スタブ
  return false;
 }
 class clickGomap implements  android.view.View.OnClickListener{
  public void onClick(View arg0) {
   Geocoder geocoder = new Geocoder(WinasMapActivity.this , Locale.getDefault());
    try {
     addressList = geocoder.getFromLocationName(address.getText().toString(), 1);
     if(addressList != null && addressList.size()>0){
      Address address = addressList.get(0);
      int lat = (int)(address.getLatitude()*1E6);
      int lng = (int)(address.getLongitude()*1E6);
      GeoPoint gp = new GeoPoint(lat,lng);
      mapview.getController().animateTo(gp);
     }
    } catch (IOException e) {
     e.printStackTrace();
    }

  }
 }
}
2.mail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
 android:id="@+id/topPoint"
    android:layout_width="50dip"
    android:layout_height="20dip"
    android:text="@string/buttonGo"
    android:layout_alignParentTop="true"
 android:visibility="invisible"
    />
<EditText
 android:id="@+id/editAdress"
    android:layout_width="150dip"
    android:layout_height="50dip"
    android:layout_below="@id/topPoint"
    android:layout_toRightOf="@id/topPoint"
/>
<Button
 android:id="@+id/buttonAdress"
 android:text="@string/buttonGo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/editAdress"
    android:layout_toRightOf="@id/editAdress"
/>
<com.google.android.maps.MapView
 android:id="@+id/theMap"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/editAdress"
    android:layout_alignParentLeft="true"
    android:apiKey="0h6yfBXg_3ti8LQTu6A3oWGdR1hTX0F5p_SpF"
    />
</RelativeLayout>

0 件のコメント: