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

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

編輯:J2ME


4.4.5
運行ControlPoints類

1、創建CopntrolPoints類文件

2、引入必要到類

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

import Java.io.IOException;

import Java.util.Enumeration;

import Javax.microedition.location.Coordinates;

import Javax.microedition.location.Landmark;

import Javax.microedition.location.LandmarkException;

import Javax.microedition.location.LandmarkStore;

3、創建ControlPoints類,並定義類中使用的常量。LandmarkStore類用來從一個永久的路標庫中存儲、刪除、恢復一個路標。

關於這個類的更多信息,參考Location API。

/**

* Model class that handles landmark store actions.

*/

public class ControlPoints

{

private LandmarkStore store = null;

private static final String STORENAME = "TOURIST_DEMO";

private static final String DEFAULT_CATEGORY = null;

private static ControlPoints INSTANCE = null;

4、創建構架。createLandmarkStore方法重新創建了一個新的路標庫。當處理陸標發生錯誤的時候程序跳轉至LandmarkException。

/**

* Constructs instance of this class. Makes sure that landmark store

* instance exists.

*/

private ControlPoints()

{

String name = null;

// Try to find landmark store called "TOURIST_DEMO".

try

{

store = LandmarkStore.getInstance(STORENAME);

}

catch (NullPointerException npe)

{

// This should never occur.

}

// Check whether landmark store exists.

if (store == null)

{

// Landmark store does not exist.

try

{

// Try to create landmark store named "TOURIST_DEMO".

LandmarkStore.createLandmarkStore(STORENAME);

name = STORENAME;

}

catch (IllegalArgumentException iae)

{

// Name is too long or landmark store with the specifIEd

// name already exists. This Exception should not occur,

// because we have earlIEr tryed to created instance of

// this landmark store.

}

catch (IOException e)

{

// Landmark store couldn't be created due to an I/O error

}

catch (LandmarkException e)

{

// Implementation does not support creating new landmark

// stores.

}

store = LandmarkStore.getInstance(name);

}

}

5、創建一個獨有的getInstance方法

/**

* Singleton patterns getInstance method. Makes sure that only one instance

* of this class is alive at once.

*

* @return instance of this class.

*/

public static ControlPoints getInstance()

{

if (INSTANCE == null)

{

INSTANCE = new ControlPoints();

}

return INSTANCE;

}

6、創建一個方法,通過索引從路標庫中尋找陸標。Landmark類代表了一個陸標。

/**

* Find a Landmark from the landmark store using a index.

*

* @param index -

* the landmark in the store.

* @return Landmark from landmark store.

*/

public Landmark findLandmark(int index)

{

Landmark lm = null;

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

int counter = 0;

while (cps.hasMoreElements())

{

lm = (Landmark) cps.nextElement();

if (counter == index)

{

break;

}

counter++;

}

return lm;

}

7、用getLandmarks方法列舉庫中所有的陸標。

GetLatitude方法和getLongtitude方法將返回坐標的經度和緯度。getQualifIEdCoordinates方法將獲取陸標的QualifIEdCoordinate值。

/**

* Find nearest landmark to coordinate parameter from the landmarkstore.

*/

public Landmark findNearestLandMark(Coordinates coord)

{

Landmark landmark = null;

double latRadius = 0.1;

double lonRadius = 0.1;

float dist = Float.MAX_VALUE;

try

{

// Generate enumeration of Landmarks that are close to coord.

Enumeration enu = store.getLandmarks(null, coord.getLatitude()

- latRadius, coord.getLatitude() + latRadius, coord

.getLongitude() - lonRadius, coord.getLongitude()

+ lonRadius);

float tmpDist;

Landmark tmpLandmark = null;

while (enu.hasMoreElements())

{

tmpLandmark = (Landmark) enu.nextElement();

tmpDist = tmpLandmark.getQualifIEdCoordinates().distance(coord);

if (tmpDist < dist)

{

landmark = tmpLandmark;

}

}

}

catch (IOException ioe)

{

// I/O error happened when Accessing the landmark store.

return null;

}

return landmark;

}

public Enumeration getLandMarks()

{

Enumeration enu = null;

try

{

enu = store.getLandmarks();

}

catch (IOException ioe)

{

// I/O error happened when Accessing the landmark store.

}

return enu;

}

8、創建變量,用於類中方法的增加、更新、刪除。

public void addLandmark(Landmark landmark) throws IOException

{

store.addLandmark(landmark, DEFAULT_CATEGORY);

}

public void updateLandmark(Landmark landmark) throws IOException,

LandmarkException

{

store.updateLandmark(landmark);

}

public void removeLandmark(Landmark landmark) throws IOException,

LandmarkException

{

store.deleteLandmark(landmark);

}

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