「
Googleマップを表示する」を参照し, Googleマップが表示できるようにする.
- MainActivity.java
- findViewByIdメソッドで, MapViewのインスタンスを取得する.
- MapView#getMapCenterメソッドで, 地図の中心位置を取得する.
- MapView#getLatitudeSpan/getLongitudeSpanメソッドで, 上辺下辺の緯度差, 左辺右辺の経度差を取得する.
- 下図のように, 地図表示範囲を計算する.
package com.moonlight_aska.android.web.googlemap03;
import android.os.Bundle;
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 = null;
@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 onGetArea(View view) {
// 地図表示範囲の計算
GeoPoint gpo = mView.getMapCenter();
double topLatitude = (gpo.getLatitudeE6() + mView.getLatitudeSpan() / 2) / 1E6;
double bottomLatitude = (gpo.getLatitudeE6() - mView.getLatitudeSpan() / 2) / 1E6;
double leftLongitude = (gpo.getLongitudeE6() - mView.getLongitudeSpan() / 2) / 1E6;
double rightLongitude = (gpo.getLongitudeE6() + mView.getLongitudeSpan() / 2) / 1E6;
Toast.makeText(this, "地図表示範囲\n緯度:" + bottomLatitude + "〜" + topLatitude +
"\n経度:" + leftLongitude + "〜" + rightLongitude , Toast.LENGTH_LONG).show();
}
}
- 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" >
<Button android:id="@+id/btnGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/getBtn"
android:onClick="onGetArea"
/>
</LinearLayout>
</RelativeLayout>
SHARP SH03C / Android 2.2
1)
2)