2013-03-21 37 views
0

我想在當前位置發生變化時運行一個方法。它會在每次當前位置更改時計算距離,然後在文本視圖中顯示它。Methods onLocationChanged

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); 

我怎麼會去使這個運行位置的變化是什麼時候?

+0

把你的代碼中onLocationChange – 2013-03-21 00:26:37

+0

我做到了這一點,它不工作 – 2013-03-21 00:29:48

+0

安置自己的onLocationChange代碼。 – 2013-03-21 00:34:00

回答

0

你應該聲明你的類爲
public MyClassName extends Activity implements LocationListener
然後把你的代碼onLocationChanged