0% found this document useful (0 votes)
157 views

Android Rotate Map Based On User Heading and Bearing While Navigation - Stack Overflow

1. Calculate the bearing between the current and next location points to determine the desired map rotation. 2. Get the user's current heading from the sensor and calculate the difference between it and the desired bearing. 3. Rotate the map by the calculated difference between the bearing and heading to align the map with the user's direction of travel.

Uploaded by

Rudolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

Android Rotate Map Based On User Heading and Bearing While Navigation - Stack Overflow

1. Calculate the bearing between the current and next location points to determine the desired map rotation. 2. Get the user's current heading from the sensor and calculate the difference between it and the desired bearing. 3. Rotate the map by the calculated difference between the bearing and heading to align the map with the user's direction of travel.

Uploaded by

Rudolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

x Dismiss

nete a la comunidad de Stack Overflow

Stack Overflow es una comunidad de 7.5 millones


de programadores como t que se ayudan
mutuamente.
nete a ellos, slo te llevar un minuto:

Registrarse

Android rotate map based on user heading and bearing while navigation

We have 2 open jobs

Learn more
Imagine yourself at Springworks

I want to rotate map while user navigation based on his direction/heading and and his current segment(current location to next). I have route
drawn on map and i am calculating bearing between two points using method current.bearingTo(destination) . I have followed this link but
results are not as i was expecting. What i want to achieve is that when user starts route navigation app should take current segment's (start
and end point) and calculate bearing between two points, also get heading/direction from sensor and rotate the map. Then on segment
completion when user takes turn (left or right) now again calculate new bearing(of next segment's start/end point) and heading direction and
rotate map accordingly. Here is my onSensorChanged method;

@Override
public void onSensorChanged( SensorEvent event ) {
Location LocationObj = LocationService.mLocation;
// If we don't have a Location, we break out
if ( LocationObj == null ) return;

float azimuth = event.values[0];


float baseAzimuth = azimuth;

GeomagneticField geoField = new GeomagneticField( Double


.valueOf( LocationObj.getLatitude() ).floatValue(), Double
.valueOf( LocationObj.getLongitude() ).floatValue(),
Double.valueOf( LocationObj.getAltitude() ).floatValue(),
System.currentTimeMillis() );

azimuth -= geoField.getDeclination(); // converts magnetic north into true north

Location destinationObj = new Location("");


destinationObj.setLongitude(33.521398);
destinationObj.setLatitude(73.090826);
// Store the bearingTo in the bearTo variable
float bearTo = LocationObj.bearingTo( destinationObj );

// If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
if (bearTo < 0) {
bearTo = bearTo + 360;
}

//This is where we choose to point it


float direction = bearTo - azimuth;

// If the direction is smaller than 0, add 360 to get the rotation clockwise.
if (direction < 0) {
direction = direction + 360;
}

//Log.i(TAG, "rotation sensor angle : " + direction);


//mapController.setMapRotation((float) Math.toRadians(direction));
//rotateImageView( arrow, R.drawable.arrow, direction );

//Set the field


String bearingText = "N";

if ( (360 >= baseAzimuth && baseAzimuth >= 337.5) || (0 <= baseAzimuth && baseAzimuth
<= 22.5) ) bearingText = "N";
else if (baseAzimuth > 22.5 && baseAzimuth < 67.5) bearingText = "NE";
else if (baseAzimuth >= 67.5 && baseAzimuth <= 112.5) bearingText = "E";
else if (baseAzimuth > 112.5 && baseAzimuth < 157.5) bearingText = "SE";
else if (baseAzimuth >= 157.5 && baseAzimuth <= 202.5) bearingText = "S";
else if (baseAzimuth > 202.5 && baseAzimuth < 247.5) bearingText = "SW";
else if (baseAzimuth >= 247.5 && baseAzimuth <= 292.5) bearingText = "W";
else if (baseAzimuth > 292.5 && baseAzimuth < 337.5) bearingText = "NW";
else bearingText = "?";

//fieldBearing.setText(bearingText);
Log.i(TAG, "rotation sensor azimuth : " + bearingText);

android google-maps rotation location android-mapview

edited May 23 at 10:33 asked Apr 26 '16 at 8:08


Community Nouman Bhatti
1 1 738 13 34

Check this SO question 6028999 and 1830391 if it can help you:) KENdi Apr 27 '16 at 6:15

@Nouman, whats the wrong result for you ? just bearingText ? Blackkara Apr 27 '16 at 14:09

Thanks for response. I got it working above i used another approach than this Nouman Bhatti Apr 28 '16
at 5:19

You might also like