Search code examples
androidlocationmanager

android didUpdateHeading (loacation manager)?


Is there an Android equivalent of didUpdateHeading (from iOS) ? Any sample code to rotate the mapview using it?

Searched a lot, but couldn't get a helpful hint.

Thanks in advance.

Edit: Let me expalain my question: In IOS, when the device changes its Heading, delegate object gets a call on its method: "didUpdateHeading". In Android I could find "onLocationChanged" to get the coordinates, but no Heading information. How do I get the heading information in Android?


Solution

  • package com.exercise.AndroidCompass;
    
    import java.util.List;
    
    import android.app.Activity;
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.widget.Toast;
    
    public class AndroidCompass extends Activity {
    
    private static SensorManager mySensorManager;
    private boolean sersorrunning;
    private MyCompassView myCompassView;
    
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
    
         myCompassView = (MyCompassView)findViewById(R.id.mycompassview);
    
         mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
         List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
    
         if(mySensors.size() > 0){
          mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
          sersorrunning = true;
          Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
    
         }
         else{
          Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
          sersorrunning = false;
          finish();
         }
     }
    
     private SensorEventListener mySensorEventListener = new SensorEventListener(){
    
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
     // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onSensorChanged(SensorEvent event) {
     // TODO Auto-generated method stub
     myCompassView.updateDirection((float)event.values[0]);
    }
     };
    

    Source: http://android-er.blogspot.com/2010/08/simple-compass-sensormanager-and.html

    http://developer.android.com/reference/android/hardware/SensorManager.html

    Should be what you're looking for. The OnSensorChanged method should provide the same functionality. (float)event.values[0] is your heading.

    Just read your bounty text. This is the only way to do it, from everything I've researched, and from my experience in general. The example I provided will give you what you need fairly quickly with copy & paste, however.