//mobile and embedded /
Developing Proximity Awareness
with the Location API
Using the Location API, you can access location data and incorporate that
data into a game.
JAVA IN ACTION
This article is the second article in a two-part series.
Part 1 described how to set up
the GameCanvas and deal with
the Mobile Sensor API, JSR-256.
We still need to add proximity
aspects to our location-based game. In
this article, we add the
For the basics of the
Location API, JSR-179,
and the Mobile
Sensor API, see my
which are listed in the
“Learn More” section
of this article. In addition, the demo apps
provided with the
Java ME SDK are very
relevant and provide
good insight into the working of
Note: The source code for the
game I develop in this two-part
series can be downloaded here.
Determining what is near you can
be done in several ways: Bluetooth
technology, Wi-Fi networks, cell
phone tower triangulation, and,
of course, GPS. What might work
in one situation might
not work best in oth-
ers. A combination of
these technologies is
perhaps the best bet
for figuring out the
location of your device.
In this second article, I concentrate on
using the Location API,
because it provides the
easiest way of finding people and items
that are near you.
Furthermore, you can
broadcast your own
location easily enough,
so others can find you as well.
is near you
of course, GPS.
Using the Location API
Listing 1 shows the initialization
code for the Location API
criteria in the constructor of
Once we have a valid location, we display that information
using the getQualifiedCoordinates
method. The location is displayed
to the user, and it is also broadcast to the server so that it can be
registered, as shown in Figure 1.
The next step is to find players near your own location with
whom you can play the game,
as shown in Listing 3. The
mock method returns a list of