0% found this document useful (0 votes)
58 views37 pages

Dispozitive Mobile de Calcul: Unit 12 - Localization Providers & Maps

The document discusses using Google Maps APIs and displaying maps in an Android application. It covers initializing Google Maps, getting the user's location, displaying markers and controls on the map. Key aspects include getting the Google Maps API key, using MapFragments, adding markers and controls, setting map types, and handling location updates via the LocationManager and LocationListener classes.
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)
58 views37 pages

Dispozitive Mobile de Calcul: Unit 12 - Localization Providers & Maps

The document discusses using Google Maps APIs and displaying maps in an Android application. It covers initializing Google Maps, getting the user's location, displaying markers and controls on the map. Key aspects include getting the Google Maps API key, using MapFragments, adding markers and controls, setting map types, and handling location updates via the LocationManager and LocationListener classes.
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/ 37

DISPOZITIVE MOBILE DE CALCUL

Unit 12 – Localization Providers & Maps


Fetch user location

Reverse geocoding using Geocoder

MapFragment
Summary Showing current location

Displaying markers

Map controls
FETCH USER CURRENT LOCATION
Google API Client
USER LOCATION
• Location service: Context.LOCATION_SERVICE
– Used for creating a LocationManager object
• Location is determined using different sources such as:
– GPS receiver;
– WiFi networks;
– Mobile cell phone;
• Permissions
– android.permission.ACCESS_FINE_LOCATION
– android.permission.ACCESS_COARSE_LOCATION
USER LOCATION
• Google Play Service evaluation and Location Service
initialization;
• Initialization of a listener by specifying:
– The source used for getting the location;
– The amount of time between two different location queries;
• Notifications received when:
– location changes;
– the source providing data changes state;
USER LOCATION

Last known
location

Current
location
Location Updates
Method arguments:
• Google API client
• Location request
• Location listener
Stopping Location Updates
LocationManager class

• Getting the last known position:


– Location getLastKnownLocation(String source)

• Gets GPS receiver state;


– GpsStatus getGpsStatus(GpsStatus status)

• Selecting the best provider based on a predefined criteria:


– getBestProvider(Criteria criteria, boolean activeProviders)
LocationListener Interface

• Receiving updates when location changes


– Based on frequency, distance

• Main approach is by using the override method:


– void onLocationChanged(Location position)
• Access coordinates by using the Location type object
Location class
• Coordinates
– getLatitude(), getLongitude(), getAltitude()
• Speed
– getSpeed()
• Time
– getTime()
• Location accuracy
– getAccuracy()
• Other infos
LocationListener example
class LocListener implements LocationListener {

@Override

public void onLocationChanged(Location position) {

/*position.getLatitude(), position.getLongitude(),
position.getAltitude(); */

}
LocationListener
• requestLocationUpdates() is used for getting the current location
• Parameters:
– The minimum time for querying updates
– The minimum distance for querying updates
• Other parameters:
– Location source:
• GPS_PROVIDER or NETWORK_PROVIDER (LocationManager)
• Selection criteria
– The LocationListener type object
• requestSingleUpdate()
Current position example
// create an object whose class implements LocationListener
LocListener locListener = new LocListener();

//getting the location service


LocationManager locManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

//requesting the current location from a location provider


locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locListener);

//removing the associated listener


locManager.removeUpdates(locListener);
Geocoder class

Latitude:
Google Inc. 37.422844
1600 Amphitheatre
Geocoding
Reverse (37°25'21.3"N)
Parkway Geocoding
Mountain View, CA 94043 Longitude:
Phone: +1 650-253-0000 -122.084342
(122°05'03.8"W)
Geocoder class
• Backend services
• Must check the availability of the service
– static method isPresent()
• Constructor initialization
– receives the Context of the application as single parameter
• INTERNET permission required
Methods

• getFromLocation(lat, long, noMaxAddresses)


• getFromLocationName()

Address Lists

Getting • Address class


• List<Address>
addresses
Address class

• getCountry()
• getCountryCode()
• getLocality()
• getAddressLine()
Getting addresses
GOOGLE MAPS
Google Maps
• Google Maps API v2
• Based on Google Maps service
• Included in Google Play services
– Found in the Extras category from SDK Manager
• Google Play services APK must be present on the mobile device
• Instruments:
– 3D maps, markers, paths, etc.
Google Maps

None

Hybrid Normal
Google
Maps
Satellite Terrain
Initialization

• Getting a valid Maps API key from Google Maps Services


– https://code.google.com/apis/console
– The key must be inserted into a valid XML resource

• Installing the Google Play services SDK


• Adding the Google Play services libraries
• Using the Google Play services libraries
Google Maps
Getting the API Key

To get one, follow this link, follow the directions and press "Create" at the
end:

https://console.developers.google.com/flows/enableapi?apiid=maps_android_backen
d&keyType=CLIENT_SIDE_ANDROID&r=E5:96:2C:F8:01:5F:04:99:62:81:3B:82:31:FC:6C:7F
:D1:B8:E4:6F%3Bcom.example.mihai.maps

You can also add your credentials to an existing key, using this line:
E5:96:2C:F8:01:5F:04:99:62:81:3B:82:31:FC:6C:7F:D1:B8:E4:6F;com.example.mihai.m
aps

Alternatively, follow the directions here:


https://developers.google.com/maps/documentation/android/start#get-key

Once you have your key (it starts with "AIza"), replace the "google_maps_key"
string in this file.
Google Maps API v2
• com.google.android.gms.maps package
• MapView
– A control which encapsulates a map
• MapFragment
– The fragment object used for displaying the map
• UiSettings
– Control for setting the map preferences
Google Maps
• Map control
• Initialization
– MapFragment#getMap() or MapView#getMap()
• Camera control
– moveCamera(CameraUpdate)
– CameraUpdateFactory
• Map type
– setMapType(mapType const)
• MAP_TYPE_NORMAL, MAP_TYPE_SATELLITE, MAP_TYPE_TERRAIN
• Settings
• getUiSettings()
Google Maps
• Circle
– Circle addCircle(CircleOptions)
• Lines
– Polyline addPolyline(PolylineOptions)
• Images
– GroundOverlay addGroundOverlay(GroundOverlayOptions)
• Polygons
– Polygon addPolygon(PolygonOptions)
Google Maps API v2 classes
• Marker
– Small items on the map showing certain locations
– Properties
• Title
• Position (coordonate) – given as LatLng (latitude and longitude)
• Icon
• etc.
– GoogleMaps#addMarker(MarkerOptions)
• MarkerOptions
– Setting the Marker options
Google Maps
• Map fragment declared as an xml layout file (res/layout)
– fragment
• android:id="@+id/map"
• android:name="com.google.android.gms.maps.MapFragment"

• The java class associated file:


setContentView(R.layout.activity_maps);
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Google Maps
• AndroidManifest.xml
– Minimum set of permissions:
• <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
– Google Maps API key
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<string name="google_maps_key">
AIzaSyeqGdwiDSbgQFTjdTpzZBZ6kN7gB8I_Jd0
</string>
Google Maps Markers
Google Maps Markers
Zoom in/out
Compass Control
My Location Button
Resourcess
• S. Komatineni, D. MacLean – Pro Android 4, Apress, 2012

• R. Meier – Professional Android 4 Application Development, Wiley, 2012

• P. Pocatilu, Programarea dispozitivelor mobile, Editura ASE, 2012

• P. Pocatilu, I. Ivan ș.a. – Programarea aplicațiilor Android, Editura, ASE,


2015

• http://developer.android.com

You might also like