Namespaces
Variants
Actions
Revision as of 09:43, 23 July 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Java ME Compass

Jump to: navigation, search
MultiMediaTile.png
Article Metadata

Code Example
Article
Created: mahbub_s60 (30 Jun 2011)
Last edited: hamishwillee (23 Jul 2012)

Contents

Introduction

This article shows how we can make Java ME compass with built in magnetic compass & GPS. If we select GPS then it will start using the GPS and calculates the direction. If we select built in magnetometer then it gets the angle from built in magnetic compass (if available). The found angle is used to rotate the compass image.

Device Orientation

Device orientation can be found by built in magnetic compass or by taking GPS location and calculating the direction of movement accordingly. The orientation angle can be used to rotate the compass image and then redrawn on the device screen. Following code shows how to calculate direction of movement using GPS location.

  private void ProcessLocation(float latitude, float longitude)
{
 
float alpha; // result
// calculate vector coordinates
float y = latitude - preLatitude;
float x = longitude - preLongitude;
 
if (y < 0.00008 && x < 0.00008)
{
return;
}
// hypotenuse
float sqrtResult = 0;
sqrtResult = (float)Math.sqrt(x * x + y * y);
float angle;
angle = (float)Math.sin(Math.abs(x) / sqrtResult);
alpha = (float)(angle * 180 / 3.14); // angle from North in degrees
// correction
if (x > 0)
{
// I or IV quadrant
if (y < 0)
{
// IV quadrant
alpha = 180 - alpha;
}
}
else
{
// II or III quadrant
if (y > 0)
{
// II quadrant
alpha = -alpha;
}
else
alpha = alpha - 180;
}
preLatitude = latitude;
preLongitude = longitude;
AngleFromNorth = (int) alpha;
angleString = Integer.toString(AngleFromNorth);
}

Test Application

In the test application, we can select GPS or Magnetic compass to find direction: File:J2meCompassegpsmagnet.zip

Related compass applications

162 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved