程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 定位API開發指南——例子:移動應用開發的定位和定位檢測(7)

定位API開發指南——例子:移動應用開發的定位和定位檢測(7)

編輯:J2ME

4.4.9 運行LandmarkEditorUI

1、創建LandmarkEditorUI類文件

2、引入必要的類

package com.nokia.example.location.tourist.ui;

import Java.io.IOException;

import Java.util.Enumeration;

import Javax.microedition.lcdui.Alert;

import Javax.microedition.lcdui.AlertType;

import Javax.microedition.lcdui.Command;

import Javax.microedition.lcdui.CommandListener;

import Javax.microedition.lcdui.Displayable;

import Javax.microedition.lcdui.Form;

import Javax.microedition.lcdui.List;

import Javax.microedition.lcdui.StringItem;

import Javax.microedition.lcdui.TextFIEld;

import Javax.microedition.location.AddressInfo;

import Javax.microedition.location.Landmark;

import Javax.microedition.location.LandmarkException;

import Javax.microedition.location.QualifIEdCoordinates;

import com.nokia.example.location.tourist.TouristMIDlet;

import com.nokia.example.location.tourist.Utils;

import com.nokia.example.location.tourist.model.ControlPoints;

import com.nokia.example.location.tourist.model.TouristData;

3、創建LandmarkEditorUI類,並將其設置為對Form的擴展,運行CommandListener,定義Form需要的TextFIEld。

/**

* VIEwer class enabling landmarks address info monifications.

*/

class LandmarkEditorUI extends Form implements CommandListener

{

// Landmark fields, (*) = required fIEld

private TextField nameField = new TextFIEld("Name (*):", "", 20,

TextFIEld.ANY);

private TextField descField = new TextFIEld("Description (*):", "", 40,

TextFIEld.ANY);

// AddressInfo fIElds

private TextField countryField = new TextFIEld("Country:", "", 20,

TextFIEld.ANY);

private TextField stateField = new TextFIEld("State:", "", 20,

TextFIEld.ANY);

private TextField cityField = new TextField("City:", "", 20, TextFIEld.ANY);

private TextField streetField = new TextFIEld("Street:", "", 20,

TextFIEld.ANY);

private TextField buildingNameField = new TextFIEld("Building name (*):",

"", 20, TextFIEld.ANY);

定義坐標信息StringITems和陸標選擇所需要的命令和常量。QualifIEdCoordinates類用經度、緯度、海拔的方式來說明坐標。

更多信息,參考Location API。

// Coordinate information

private StringItem lat = new StringItem("Lat:", "");

private StringItem lon = new StringItem("Lon:", "");

private StringItem alt = new StringItem("Alt:", "");

/** Landmark selector list */

private List list = new List("Landmarks:", List.EXCLUSIVE);

private Command saveNewCmd = new Command("Save", Command.OK, 1);

private Command saveUpdatedCmd = new Command("Save", Command.OK, 1);

private Command updateCmd = new Command("Update", Command.OK, 1);

private Command removeCmd = new Command("Remove", Command.OK, 1);

private Command listCmd = new Command("List", Command.OK, 1);

private Command routeCmd = new Command("Route", Command.BACK, 1);

public static final int MODE_UPDATE = 0;

public static final int MODE_ADDNEW = 1;

private QualifIEdCoordinates coord = null;

private Displayable route = null;

private TouristData data = null;

4、建類結構

/**

* Construct Landmark Editor UI Form.

*

* @param route -

* a reference of Route UI.

* @param data -

* a reference to TouristData model layer class.

*/

public LandmarkEditorUI(Displayable route, TouristData data)

{

super("Landmark Editor");

this.route = route;

this.data = data;

// route and list commands always available on landmark editor

addCommand(routeCmd);

addCommand(listCmd);

setCommandListener(this);

// route command always available on landmarks list

list.addCommand(routeCmd);

list.setCommandListener(this);

}

5、建方法,初始化UI組件,並顯示路標編輯器。GetAltitude方法返回這個組件坐標的經度值!

/**

* Initialize

public void showEditor(QualifIEdCoordinates newCoord, int mode)

{

this.coord = newCoord;

// initialize coordinate values

lat.setText(Utils.formatDouble(newCoord.getLatitude(), 3));

lon.setText(Utils.formatDouble(newCoord.getLongitude(), 3));

alt.setText(Utils.formatDouble(newCoord.getAltitude(), 1));

// initialize editable test fIEld values

nameFIEld.setString("");

descFIEld.setString("");

countryFIEld.setString("");

stateFIEld.setString("");

cityFIEld.setString("");

streetFIEld.setString("");

buildingNameFIEld.setString("");

deleteAll();

append(nameFIEld);

append(descFIEld);

append(countryFIEld);

append(stateFIEld);

append(cityFIEld);

append(streetFIEld);

append(buildingNameFIEld);

append(lat);

append(lon);

append(alt);

// Update existing landmark.

if (mode == MODE_UPDATE)

{

Landmark lm = ControlPoints.getInstance().findNearestLandMark(

newCoord);

if (lm != null)

{

nameFIEld.setString(lm.getName());

descFIEld.setString(lm.getDescription());

AddressInfo info = lm.getAddressInfo();

if (info != null)

{

countryField.setString(info.getFIEld(AddressInfo.COUNTRY));

stateField.setString(info.getFIEld(AddressInfo.STATE));

cityField.setString(info.getFIEld(AddressInfo.CITY));

streetField.setString(info.getFIEld(AddressInfo.STREET));

buildingNameFIEld.setString(info

.getFIEld(AddressInfo.BUILDING_NAME));

}

}

removeCommand(updateCmd);

removeCommand(saveNewCmd);

addCommand(saveUpdatedCmd);

addCommand(listCmd);

}

// Add new landmark to the landmark store.

else if (mode == MODE_ADDNEW)

{

removeCommand(updateCmd);

removeCommand(saveUpdatedCmd);

addCommand(saveNewCmd);

addCommand(listCmd);

}

TouristMIDlet.getDisplay().setCurrent(this);

}

6、建方法,顯示陸標選擇列表。

/**

* Show landmark selector List. Content of the list is refreshed each time

* this method is called.

*/

public void showList()

{

list.deleteAll();

Landmark lm = null;

Enumeration landmarks = ControlPoints.getInstance().getLandMarks();

// Check whether landmarks can be founds from the Landmark store.

if (landmarks != null)

{

while (landmarks.hasMoreElements())

{

lm = ((Landmark) landmarks.nextElement());

list.append(lm.getName(), null);

}

list.addCommand(updateCmd);

list.addCommand(removeCmd);

}

// No landmarks found (list is empty)

else

{

list.removeCommand(updateCmd);

list.removeCommand(removeCmd);

}

TouristMIDlet.getDisplay().setCurrent(list);

}

7、建方法,檢測所必需的區域並不缺少。

/**

* Check whether any required fields are missing. (*) on the TextFIElds label

* indicates that fIEld is mandatory.

*

* @return Name of the missing fIEld name or null indicating no required

* fIElds are missing.

*/

private String checkRequiredFIElds()

{

if (nameFIEld.getString().equals(""))

{

return "Name";

}

else if (nameFIEld.getString().equals(""))

{

return "Description";

}

else if (buildingNameFIEld.getString().equals(""))

{

return "Building name";

}

return null;

}

8、建方法,通過UI組件繼承路標。

/**

* Generate landmark from UI component values.

*

* @return Genereated Landmark.

*/

private Landmark generateLandmark()

{

String field = checkRequiredFIElds();

if (fIEld != null)

{

Alert alert = new Alert("Error", "Value for required fIEld "

+ fIEld + " is missing.", null, AlertType.ERROR);

alert.setTimeout(3000); // 3 secs

TouristMIDlet.getDisplay().setCurrent(alert, this);

return null;

}

AddressInfo info = new AddressInfo();

info.setField(AddressInfo.COUNTRY, countryFIEld.getString());

info.setField(AddressInfo.STATE, stateFIEld.getString());

info.setField(AddressInfo.CITY, cityFIEld.getString());

info.setField(AddressInfo.STREET, streetFIEld.getString());

info.setField(AddressInfo.BUILDING_NAME, buildingNameFIEld.getString());

Landmark lm = new Landmark(nameFIEld.getString(),

descFIEld.getString(), coord, info);

return lm;

}

9、測按鍵輸入

/**

* Event indicating when a command button is pressed.

*

* @see Javax.microedition.lcdui.CommandListener#commandAction

(Javax.microedition.lcdui.Command,

* Javax.microedition.lcdui.Displayable)

*/

public void commandAction(Command command, Displayable displayable)

{

Landmark landmark = null;

// Add new Landmark to the LandmarkStore

if (command == saveNewCmd)

{

landmark = generateLandmark();

if (landmark != null)

{

try

{

ControlPoints.getInstance().addLandmark(landmark);

data.createProximityListener(coord);

}

catch (IOException e)

{

Alert alert = new Alert("Error",

"I/O Error during add Operation", null,

AlertType.ERROR);

alert.setTimeout(3000); // 3 secs

TouristMIDlet.getDisplay().setCurrent(alert);

}

// New landmark is available on the list

showList();

}

}

// Update existing landmark

else if (command == saveUpdatedCmd)

{

landmark = generateLandmark();

if (landmark != null)

{

try

{

ControlPoints.getInstance().updateLandmark(landmark);

data.createProximityListener(coord);

}

catch (IOException e)

{

Alert alert = new Alert("Error",

"I/O Error during update Operation", null,

AlertType.ERROR);

alert.setTimeout(3000); // 3 secs

TouristMIDlet.getDisplay().setCurrent(alert);

}

catch (LandmarkException e)

{

// Landmark instance passed as the parameter does not

// belong to this LandmarkStore or does not exist in the

// store any more.

Alert alert = new Alert("Error",

"Unexpected error: can not find landmark from "

+ "the landmark store.", null,

AlertType.ERROR);

alert.setTimeout(3000); // 3 secs

TouristMIDlet.getDisplay().setCurrent(alert);

}

// Updated landmark is available on the list

showList();

}

}

// Go back to the previous screen

else if (command == routeCmd)

{

TouristMIDlet.getDisplay().setCurrent(route);

}

// Show landmark editor for the selected landmark.

else if (command == updateCmd)

{

int index = list.getSelectedIndex();

landmark = ControlPoints.getInstance().findLandmark(index);

showEditor(landmark.getQualifIEdCoordinates(), MODE_UPDATE);

}

// Remove selected Landmark from LandmarkStore

else if (command == removeCmd)

{

try

{

int index = list.getSelectedIndex();

landmark = ControlPoints.getInstance().findLandmark(index);

ControlPoints.getInstance().removeLandmark(landmark);

Alert alert = new Alert("Information",

"Landmark removed successfully.", null, AlertType.INFO);

alert.setTimeout(3000); // 3 secs

TouristMIDlet.getDisplay().setCurrent(alert);

}

catch (IOException e)

{

Alert alert = new Alert("Error", "I/O Error during Operation",

null, AlertType.ERROR);

alert.setTimeout(3000); // 3 secs

TouristMIDlet.getDisplay().setCurrent(alert);

}

catch (LandmarkException le)

{

Alert alert = new Alert("Error",

"landmark can not be found from the landmark store.",

null, AlertType.ERROR);

alert.setTimeout(3000); // 3 secs

TouristMIDlet.getDisplay().setCurrent(alert);

}

showList();

}

// Show the list of Landmarks

else if (command == listCmd)

{

showList();

}

}

}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved