最終更新:
moonlight_aska 2014年02月23日(日) 11:22:50履歴
Googleマップのカメラ位置(表示範囲等)が変わった時に何か処理を行いたい場合には, OnCameraChangeListenerインタフェースを利用する.
「Googleマップを表示する」を参照し, Googleマップが表示できるようにする.

- MainActivity.java
- GoogleMapのインスタンスを取得する.
- GoogleMap#setsetOnCameraChangeListenerメソッドで, イベントハンドラを登録する.
- カメラ位置変更時に呼ばれるonCameraChangeメソッドに, カメラ位置変更時の処理を実装する.
package com.moonlight_aska.android.web.googlemap005;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.VisibleRegion;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements OnCameraChangeListener {
private GoogleMap mMap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMap = ( (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map) ).getMap();
if (mMap != null) {
// イベントハンドラの登録
mMap.setOnCameraChangeListener(this);
}
}
@Override
public void onCameraChange(CameraPosition position) {
// TODO Auto-generated method stub
// カメラ位置変更時の処理を実装
Projection proj = mMap.getProjection();
VisibleRegion vRegion = proj.getVisibleRegion();
// 北東 = top/right, 南西 = bottom/left
double topLatitude = vRegion.latLngBounds.northeast.latitude;
double bottomLatitude = vRegion.latLngBounds.southwest.latitude;
double leftLongitude = vRegion.latLngBounds.southwest.longitude;
double rightLongitude = vRegion.latLngBounds.northeast.longitude;
Toast.makeText(this, "地図表示範囲\n緯度:" + bottomLatitude + "〜" + topLatitude +
"\n経度:" + leftLongitude + "〜" + rightLongitude , Toast.LENGTH_LONG).show();
}
}
- 動作例

コメントをかく