0% found this document useful (0 votes)
29 views23 pages

ITE 2152 Introduction To Mobile Application Development: Week 11

This document discusses location-based services and mobile application development. It covers displaying maps using Google Maps, obtaining location data from GPS, cell towers and Wi-Fi, and using the LocationManager class to get location updates and monitor specific locations. The document provides code examples for displaying maps, changing map views, navigating to locations, and performing geocoding and reverse geocoding to get addresses from coordinates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views23 pages

ITE 2152 Introduction To Mobile Application Development: Week 11

This document discusses location-based services and mobile application development. It covers displaying maps using Google Maps, obtaining location data from GPS, cell towers and Wi-Fi, and using the LocationManager class to get location updates and monitor specific locations. The document provides code examples for displaying maps, changing map views, navigating to locations, and performing geocoding and reverse geocoding to get addresses from coordinates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

ITE 2152

Introduction to Mobile
Application Development
Week 11

M P C Sandaru – Software Engineer


Location based services
• Location-Based Services, commonly known as
LBS.
• LBS apps track your location, and might offer
additional services such as locating amenities
nearby, offering suggestions for route
planning, and so on.
• Of course, one of the key ingredients in an LBS
app is maps, which present a visual
representation of your location.
Displaying Maps
• Google Maps is one of the many applications bundled
with the Android platform.
• In addition to simply using the Maps application, you
can also embed it into your own applications and make
it do some very cool things.
• This section describes how to use Google Maps in your
Android applications and programmatically perform
the following:
– Change the views of Google Maps
– Obtain the latitude and longitude of locations in Google
Maps
– Perform geocoding and reverse geocoding (translating an
address to latitude and longitude and vice versa)
Creating an Project
• To get started, you need to first create an
Android project so that you can display Google
Maps in your activity:
1. Using Android Studio, create an Android
project and name it LBS.
2. From the Create New Project Wizard, select
Google Maps Activity as shown in Figure 10-1
Obtaining Maps API
• Beginning with the Android SDK release v1.0, you need to
apply for a free Google Maps API key before you can
integrate Google Maps into your Android application.
• When you apply for the key, you must also agree to
Google’s terms of use, so be sure to read them carefully.
• To get a Google Maps key, open the google_maps_api.xml
file that was created in your LBS project.
• Within this file is a link to create a new Google Maps key.
• Simply copy and paste the link into your browser and follow
the instructions.
• Make note of the key that Google gives you because you
need it later in our projects.
Example
Example
• To display Google Maps in your application, you first need
the ACCESS_FINE_LOCATION permission in your manifest
file.
• This is created for you automatically when you selected to
set up a Google Maps Activity.
• You can see the line if you open the AndroidManifest.xml.
• <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATIO
N" />
• In order to test your application on the Android emulator,
be sure to create an Emulator with an SDK version that
includes Google Play Services as the selected target.
Display Zoom control
• The previous section showed how you can display
Google Maps in your Android application.
• You can pan the map to any desired location and
it updates on-the-fly.
• However, there is no way to use the emulator to
zoom in or out from a particular location (on a
real Android device you can pinch the map to
zoom it).
• Thus, in this section, you find out how you can
enable users to zoom in or out of the map using
the built-in zoom controls.
Displaying the Buit-in Zoom Controls
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.jfdimarzio.locationservices.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment“
map:uiZoomControls="true"
/>
Example
Changing Views
• By default, Google Maps is displayed in map view, which is
basically drawings of streets and places of interest.
• You can also set Google Maps to display in satellite view
using the setMapType()method of the GoogleMap class:
– public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title(
"Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
Example
Navigating to a Specific Location
• By default, Google Maps displays the map of Australia when it is first loaded.
However, you can set Google Maps to display a particular location. To do so, you
can use the moveCamera() method of the GoogleMap class.
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng boston = new LatLng(42.3601, -71.0589);
mMap.addMarker(new MarkerOptions().position(boston).title( “
Boston, Mass"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(boston));
}

• 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!

You might also like