現在の場所が変更されたときにメソッドを実行しようとしています。現在地が変わるたびに距離を計算し、テキストビューに表示します。
public GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
locationManager.requestLocationUpdates(provider, 20000, 0, (LocationListener) this);
Location marker = new Location("");
marker.setLatitude(lat);
marker.setLongitude(long1);
if(select==false){
TextView t = (TextView) findViewById(R.id.tv_location);
t.setText("Please select a marker" + lat + long1);
Toast.makeText(this, "not selected", Toast.LENGTH_LONG).show();
} else if(select==true){
float distanceBetweenPoints = location.distanceTo(marker);
TextView t = (TextView) findViewById(R.id.tv_location);
t.setText("Distance to station is "+ distanceBetweenPoints);
Toast.makeText(this, "Distance is "+distanceBetweenPoints, Toast.LENGTH_LONG).show();
if(distanceBetweenPoints<1000){
//do something
}
}
}
そして onInfoWindow コード
googleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
public void onInfoWindowClick(Marker marker) {
LatLng clickedMarkerLatLng = marker.getPosition();
lat = clickedMarkerLatLng.latitude;
long1 = clickedMarkerLatLng.longitude;
String name = marker.getTitle();
select=true;
TextView t = (TextView) findViewById(R.id.tv_location);
t.setText("Location of "+ name +" is "+ lat + " by " + long1);
場所が変わったときにこれを実行するにはどうすればよいですか?