J2ME GPS 定位及使用源代码

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;

class MyCanvas extends Canvas
{

    GISMIDlet GIS;

    public MyCanvas(GISMIDlet GIS)
    {
        this.GIS = GIS;
    }
    private double lat;
    private double lon;

    public void setInfo(double lat, double lon)
    {
        this.lat = lat;
        this.lon = lon;
    }

    protected void paint(Graphics g)
    {
// TODO 自动产生方法 Stub
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                Font.SIZE_SMALL);
        g.setFont(f);

// use font height as a line height
        int lineHeight = f.getHeight();
// current line counter
        int line = 0;
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x0000ff);

        g.drawString("Lat, Lon (" + GIS.formatDouble(lat, 5) + ", " + GIS.formatDouble(lon, 5) + ")", 0, lineHeight * (line++), Graphics.TOP | Graphics.LEFT);

    }
}

public class GISMIDlet extends MIDlet implements CommandListener, LocationListener
{

    private Command exitCmd = new Command("Exit", Command.EXIT, 1);

//private Form mainForm;
    private MyCanvas myCanvas;
    protected double lat; // 纬度
    protected double lon; // 经度

//private LocationProvider locationProvider;

//private TextField latField; // 緯度欄位

//private TextField lonField; // 經度欄位
    public GISMIDlet()
    {
        /* mainForm = new Form("GIS 經緯度測試");
        latField = new TextField("緯度:", formatDouble(lat, 5), 15,
        TextField.DECIMAL); // 設定緯度欄位格式
        lonField = new TextField("經度:", formatDouble(lon, 5), 15,
        TextField.DECIMAL); // 設定經度欄位格式
        mainForm.append(latField); // 增加緯度欄位
        mainForm.append(lonField); // 增加經度欄位
        mainForm.addCommand(exitCmd); // 增加軟體按鈕
        mainForm.setCommandListener(this); // 監聽按鈕事件 */
        myCanvas = new MyCanvas(this);
        myCanvas.addCommand(exitCmd);
        myCanvas.setCommandListener(this);
        Display.getDisplay(this).setCurrent(myCanvas); // 顯示畫面
    }

    protected void startApp() throws MIDletStateChangeException
    {

        Criteria c1 = new Criteria(); // 存取座標之限制
        c1.setHorizontalAccuracy(500); // 水平
        c1.setVerticalAccuracy(25);
        /* try {
        while (true) {
        Thread.sleep(5000); // 每幾秒顯示一次資料 */
        try
        {
            LocationProvider lp = LocationProvider.getInstance(c1); // 讀取座標之物件
            if (lp != null)
            {
                lp.setLocationListener(this, -1, 0, 0); // 每幾秒讀取一次座標

            }
            Location LC = lp.getLocation(60);
            Coordinates C = LC.getQualifiedCoordinates(); // 將座標封存

            if (C != null)
            {
                lat = C.getLatitude(); // 緯度
                lon = C.getLongitude(); // 經度
                myCanvas.setInfo(lat, lon);
            }
        }
        catch (LocationException e)
        {
            e.printStackTrace();
        }
        catch (InterruptedException e)
        {
// TODO 自動產生 catch 區塊
            e.printStackTrace();
        }

    } // end method startApp

    protected void pauseApp()
    {
// TODO 自動產生方法 Stub

    } // end method pauseApp

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
    {
// TODO 自動產生方法 Stub

    } // end method destroyApp

    public void commandAction(Command c, Displayable arg1)
    {
        if (c == exitCmd)
        {
            try
            {
                destroyApp(false);
            }
            catch (MIDletStateChangeException exception)
            {
                System.out.println("MIDletStateChangeException");
            }
            notifyDestroyed();
        }

    } // end method commandAction

    public String formatDouble(double value, int decimals)
    {
        String doubleStr = "" + value;
        int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".")
                : doubleStr.indexOf(",");
// Decimal point can not be found...
        if (index == -1)
        {
            return doubleStr;
        }
// Truncate all decimals
        if (decimals == 0)
        {
            return doubleStr.substring(0, index);
        }

        int len = index + decimals + 1;
        if (len >= doubleStr.length())
        {
            len = doubleStr.length();
        }

        double d = Double.parseDouble(doubleStr.substring(0, len));
        return String.valueOf(d);
    } // end method formatDouble

    public void locationUpdated(LocationProvider lp, final Location location)
    {
        new Thread()
        {

            public void run()
            {
                if (location != null && location.isValid())
                {
                    Coordinates C = location.getQualifiedCoordinates();
                    if (C != null)
                    {
                        lat = C.getLatitude(); // 緯度
                        lon = C.getLongitude(); // 經度
                        myCanvas.setInfo(lat, lon);
                        myCanvas.repaint();
                    }

                }

            }
        }.start();
    }

    public void providerStateChanged(LocationProvider arg0, int arg1)
    {
// TODO 自動產生方法 Stub

    }

    /*public void updata(double lat, double lon) {
    latField.setLabel("緯度:" + formatDouble(lat, 5)); // 更新緯度欄位
    lonField.setLabel("經度:" + formatDouble(lon, 5)); // 更新經度欄位
    latField.setString("" + formatDouble(lat, 5)); // 更新緯度欄位
    lonField.setString("" + formatDouble(lon, 5)); // 更新經度欄位
    } // end method updata*/
} // end class GISMIDlet

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值