ControlPoints.java
/*
* Copyright © 2012 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.location.touristroute.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;
/**
* 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;
/**
* 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);
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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);
}
}