5

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?

zolio
  • 2,439
  • 4
  • 22
  • 34

2 Answers2

3
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.

Jack Satriano
  • 1,999
  • 1
  • 13
  • 17
  • Thaks a lot Jack57. I implemented it with your code. Works perfect. I am getting the Andlge in degree in the event.values[0] above. – zolio Jul 05 '12 at 23:24
2

If you need the heading in real time, then you should go with @jack57's answer and use the G-sensors in the device.

If you only need the heading when you get a new location, then you can get it with Location.getBearing(), but note that this only works on GPS coordinates, if you got your coordinate from LocationManager.NETWORK_PROVIDER, then the bearing will be 0.

Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • Thank you. I was looking for something like this for my code. But I need continuous update of the bearing, which I understand cannot get from this method. But this will be helpful in some other places. – zolio Jul 05 '12 at 23:27