Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 05:19, 26 June 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Java ME Location API(日本語)

From Wiki
Jump to: navigation, search
Article Metadata
Article
Translated:
By fnjwikimng
Last edited: hamishwillee (26 Jun 2013)


Location API(JSR-179)は、緯度・経度といった位置情報を得るために使われます。

コード例を、以下に示します。

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
 
 
public class loctest extends MIDlet implements CommandListener
{
private Display display;
private Form form;
private Command cmdExit,cmdOK;
private StringItem si;
 
public loctest()
{
display = Display.getDisplay(this);
form = new Form("Location Api test");
cmdExit = new Command("Exit",Command.EXIT,5);
cmdOK = new Command("OK",Command.OK,1);
si = new StringItem("Geo Location", "Click OK");
form.append(si);
form.addCommand(cmdOK);
form.addCommand(cmdExit);
form.setCommandListener(this);
}
 
public void startApp()
{
display.setCurrent(form);
}
 
public void pauseApp()
{
}
 
public void destroyApp(boolean flag)
{
notifyDestroyed();
}
 
public void commandAction(Command c, Displayable d)
{
if (c == cmdOK){
Retriever ret = new Retriever(this);
ret.start();
} else if (c == cmdExit) {
destroyApp(false);
}
}
 
public void displayString(String string)
{
si.setText(string);
}
}
 
 
class Retriever extends Thread
{
private loctest midlet;
public Retriever(loctest midlet)
{
this.midlet = midlet;
}
 
public void run()
{
try
{
checkLocation();
}
catch (Exception ex)
{
ex.printStackTrace();
midlet.displayString(ex.toString());
}
}
 
public void checkLocation() throws Exception
{
String string;
Location l;
LocationProvider lp;
Coordinates c;
 
Criteria cr= new Criteria();
cr.setHorizontalAccuracy(500);
 
lp= LocationProvider.getInstance(cr);
l = lp.getLocation(60);
 
c = l.getQualifiedCoordinates();
if(c != null ) {
// Use coordinate information
double lat = c.getLatitude();
double lon = c.getLongitude();
string = "\nLatitude : " + lat + "\nLongitude : " + lon;
} else {
string ="Location API failed";
}
midlet.displayString(string);
}
}
This page was last modified on 26 June 2013, at 05:19.
154 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×