ITE 2152 Introduction To Mobile Application Development: Week 11
ITE 2152 Introduction To Mobile Application Development: Week 11
Introduction to Mobile
Application Development
Week 11
• In the preceding code, a LatLng object is created and set to the new coordinates
of 42.3601 N, 71.0589 W (the coordinates of Boston). Notice that to represent
either W (west) or S (south) you use the negative value of the coordinate.
Example
Geocoding & Reverse Geocoding
• If you know the latitude and longitude of a location, you can
find out its address using a process known as reverse
geocoding. Google Maps in Android supports reverse
geocoding via the Geocoder class.
• The following code snippet shows how you can retrieve the
address of a location just touched using the getFromLocation()
method:
• The Geocoder object converts the latitude and longitude into
an address using the getFromLocation() method.
• After the address is obtained, you display it using the Toast
class.
• Keep in mind that pin will not move.
• In this example, we are only getting the address of a location
that you touch. Figure shows the application displaying the
address of a location that was touched on the map.
Example
Getting Location Data
• Nowadays, mobile devices are commonly
equipped with GPS receivers.
• Because of the many satellites orbiting the
earth, you can use a GPS receiver to find your
location easily.
• However, GPS requires a clear sky to work and
hence does not always work indoors or where
satellites can’t penetrate (such as a tunnel
through a mountain).
Cell tower triangulation
• Another effective way to locate your position is through cell
tower triangulation.
• When a mobile phone is switched on, it is constantly in contact
with base stations surrounding it.
• By knowing the identity of cell towers, it is possible to translate
this information into a physical location through the use of
various databases containing the cell towers’ identities and their
exact geographical locations.
• The advantage of cell tower triangulation is that it works indoors,
without the need to obtain information from satellites.
• However, it is not as precise as GPS because its accuracy depends
on overlapping signal coverage, which varies quite a bit.
• Cell tower triangulation works best in densely populated areas
where the cell towers are closely located.
WIFI Triangulation
• A third method of locating your position is to rely
on Wi-Fi triangulation.
• Rather than connect to cell towers, the device
connects to a Wi-Fi network and checks the
service provider against data-bases to determine
the location serviced by the provider.
• Of the three methods described here, Wi-Fi
triangulation is the least accurate.
• On the Android platform, the SDK provides the
LocationManager class to help your device deter-
mine the user’s physical location.
LocationManager class
• In Android, location-based services are provided by the
LocationManager class, located in the android.location package.
• Using the LocationManager class, your application can obtain
periodic updates of the device’s geographical locations, as well as
fire an intent when it enters the proximity of a certain location.
• In the MapsActivity.java file, you first check for permission to use
the Course Locations.
• Then you obtain a reference to the LocationManager class using the
getSystemService() method.
• You do this in the onCreate() method of the LBSActivity:
//---use the LocationManager class to obtain locations data---
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
MyLocationListner class
• Next, you create an instance of the MyLocationListener class, which
you define later in the class.
• The MyLocationListener class implements the LocationListener
abstract class.
• You need to over-ride four methods in this implementation:
➤➤onLocationChanged(Location location)—Called when the
location has changed
➤➤onProviderDisabled(String provider)—Called when the
provider is disabled by the user
➤➤onProviderEnabled(String provider)—Called when the provider is
enabled by the user
➤➤onStatusChanged(String provider, int status, Bundle extras)—
Called when the provider status changes
Monitor a location
• One very cool feature of the LocationManager class is its
ability to monitor a specific location. This is achieved
using the addProximityAlert() method.
• The addProximityAlert() method takes five arguments:
– Latitude
– Longitude
– Radius (in meters)
– Expiration (duration for which the proximity alert is valid,
after which it is deleted; –1 for no expiration)
– Pending intent
Note that if the Android device’s screen goes to sleep, the
proximity is also checked once every four minutes in order to
preserve the battery life of the device
Thank You!