SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
4
 Location API
 Anuchit Chalothorn
 anoochit@gmail.com

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Determine the current location
Most Android devices allow to determine the
current geolocation. This can be done via a
GPS (Global Positioning System) module, via
cell tower triangulation or via wifi networks.




Ref: http://developer.android.com/guide/topics/location/index.html
Location Manager
The LocationManager class provides access to
the Android location service. This services
allows to access location providers, to register
location update listeners and proximity alerts
and more.
LocationProvider
The LocationProvider class is the superclass of
the different location providers which deliver the
information about the current location. This
information is stored in the Location class.
LocationProvider

LocationProvider   Description

network            Uses the mobile network or WI-Fi to determine the best location. Might have
                   a higher precision in closed rooms then GPS.

gps                Use the GPS receiver in the Android device to determine the best location
                   via satellites. Usually better precision than network.

passive            Allows to participate in location of updates of other components to save
                   energy
Selecting LocationProvider via Criteria

For a flexible selection of the best location
provider use a Criteria object, in which you can
define how the provider should be selected.
Permission
If you want to access the GPS sensor, you
need the ACCESS_FINE_LOCATION
permission. Otherwise you need the
ACCESS_COARSE_LOCATION permission.
Prompt the user to Enabled GPS

LocationManager service = (LocationManager) getSystemService
(LOCATION_SERVICE);
boolean enabled = service
  .isProviderEnabled(LocationManager.GPS_PROVIDER);

if (!enabled) {
  Intent intent = new Intent(Settings.
ACTION_LOCATION_SOURCE_SETTINGS);
  startActivity(intent);
}
Workshop: Get current location
Use criteria find the best provider then get
current position from it.

LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider
(criteria, true);
Location location = locationManager.getLastKnownLocation
(provider);
float lat = (float) location.getLatitude();
       float lon = (float) location.getLongitude();
Install Google Play services
Google Maps
Google provides via Google play a library for
using Google Maps in your application. The
following description is based on the Google
Maps Android API v2 which provides significant
improvements to the older API version.




Ref: https://developers.google.com/maps/documentation/android/
Getting the Google Map key
To use Google Maps you need to create a valid
Google Maps API key. The key is free, you can
use it with any of your applications that call the
Maps API, and it supports an unlimited number
of users.
SHA-1 for your signature key

keytool -list -v -alias androiddebugkey -
keystore ~/.android/debug.keystore -
storepass android -keypass android
Work with Google Map
● Register your app and sha1 for publish
  keystore and debug keystore
● Import Google Play Service library
● Add some permission and information
● Add MapView to layout
● then coding
MapView
The library provides the com.google.android.
gms.maps.MapFragment class and the
MapView class for displaying the map
component.

<fragment
       android:id="@+id/map"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       class="com.google.android.gms.maps.MapFragment" />
Permission
API Key
Map & Marker

static final LatLng SIPA = new LatLng(13.885245,100.565586);
GoogleMap map = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
if (map!=null) {
    Marker sipa = map.addMarker(new
    MarkerOptions().position(SIPA).title("SIPA"));
}
End

Contenu connexe

En vedette

Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsAhsanul Karim
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIAhsanul Karim
 
Day 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver ComponentDay 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver ComponentAhsanul Karim
 
Day 15: Working in Background
Day 15: Working in BackgroundDay 15: Working in Background
Day 15: Working in BackgroundAhsanul Karim
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layoutKrazy Koder
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesAhsanul Karim
 
Lecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesLecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesAhsanul Karim
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Ahsanul Karim
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMDong-Ho Lee
 

En vedette (12)

Android UI Development
Android UI DevelopmentAndroid UI Development
Android UI Development
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts API
 
Day 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver ComponentDay 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver Component
 
Day 15: Working in Background
Day 15: Working in BackgroundDay 15: Working in Background
Day 15: Working in Background
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layout
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through Activities
 
Lecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesLecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & Preferences
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
 
Advance Android Layout Walkthrough
Advance Android Layout WalkthroughAdvance Android Layout Walkthrough
Advance Android Layout Walkthrough
 
Android ppt
Android pptAndroid ppt
Android ppt
 

Similaire à Android App Development 04 : Location API

Mobile Application Development-Lecture 15 & 16.pdf
Mobile Application Development-Lecture 15 & 16.pdfMobile Application Development-Lecture 15 & 16.pdf
Mobile Application Development-Lecture 15 & 16.pdfAbdullahMunir32
 
[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based ServicesNikmesoft Ltd
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on AndroidJomar Tigcal
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentIJERD Editor
 
Location based services 10
Location based services   10Location based services   10
Location based services 10Michael Shrove
 
Android App Development - 14 location, media and notifications
Android App Development - 14 location, media and notificationsAndroid App Development - 14 location, media and notifications
Android App Development - 14 location, media and notificationsDiego Grancini
 
Android location and sensors API
Android location and sensors APIAndroid location and sensors API
Android location and sensors APIeleksdev
 
Synapseindia android application development tutorial
Synapseindia android application development tutorialSynapseindia android application development tutorial
Synapseindia android application development tutorialSynapseindiappsdevelopment
 
Synapseindia android apps development tutorial
Synapseindia android apps  development tutorialSynapseindia android apps  development tutorial
Synapseindia android apps development tutorialSynapseindiappsdevelopment
 
How to use geolocation in react native apps
How to use geolocation in react native appsHow to use geolocation in react native apps
How to use geolocation in react native appsInnovationM
 
Matchinguu droidcon presentation
Matchinguu droidcon presentationMatchinguu droidcon presentation
Matchinguu droidcon presentationDroidcon Berlin
 
Smart Way to Track the Location in Android Operating System
Smart Way to Track the Location in Android Operating SystemSmart Way to Track the Location in Android Operating System
Smart Way to Track the Location in Android Operating SystemIOSR Journals
 
What's new in Android O
What's new in Android OWhat's new in Android O
What's new in Android OKirill Rozov
 
Android location
Android locationAndroid location
Android locationKrazy Koder
 

Similaire à Android App Development 04 : Location API (20)

Mobile Application Development-Lecture 15 & 16.pdf
Mobile Application Development-Lecture 15 & 16.pdfMobile Application Development-Lecture 15 & 16.pdf
Mobile Application Development-Lecture 15 & 16.pdf
 
MAD Unit 6.pptx
MAD Unit 6.pptxMAD Unit 6.pptx
MAD Unit 6.pptx
 
Android - Android Geocoding and Location based Services
Android - Android Geocoding and Location based ServicesAndroid - Android Geocoding and Location based Services
Android - Android Geocoding and Location based Services
 
Location based services
Location based servicesLocation based services
Location based services
 
[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on Android
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Location based services 10
Location based services   10Location based services   10
Location based services 10
 
Android App Development - 14 location, media and notifications
Android App Development - 14 location, media and notificationsAndroid App Development - 14 location, media and notifications
Android App Development - 14 location, media and notifications
 
Android location and sensors API
Android location and sensors APIAndroid location and sensors API
Android location and sensors API
 
Synapseindia android application development tutorial
Synapseindia android application development tutorialSynapseindia android application development tutorial
Synapseindia android application development tutorial
 
Synapseindia android apps development tutorial
Synapseindia android apps  development tutorialSynapseindia android apps  development tutorial
Synapseindia android apps development tutorial
 
How to use geolocation in react native apps
How to use geolocation in react native appsHow to use geolocation in react native apps
How to use geolocation in react native apps
 
Matchinguu droidcon presentation
Matchinguu droidcon presentationMatchinguu droidcon presentation
Matchinguu droidcon presentation
 
Smart Way to Track the Location in Android Operating System
Smart Way to Track the Location in Android Operating SystemSmart Way to Track the Location in Android Operating System
Smart Way to Track the Location in Android Operating System
 
Android Location and Maps
Android Location and MapsAndroid Location and Maps
Android Location and Maps
 
What's new in Android O
What's new in Android OWhat's new in Android O
What's new in Android O
 
Html5 geolocation api
Html5 geolocation apiHtml5 geolocation api
Html5 geolocation api
 
MyATM
MyATMMyATM
MyATM
 
Android location
Android locationAndroid location
Android location
 

Plus de Anuchit Chalothorn (20)

Flutter Workshop 2021 @ ARU
Flutter Workshop 2021 @ ARUFlutter Workshop 2021 @ ARU
Flutter Workshop 2021 @ ARU
 
Flutter workshop @ bang saen 2020
Flutter workshop @ bang saen 2020Flutter workshop @ bang saen 2020
Flutter workshop @ bang saen 2020
 
13 web service integration
13 web service integration13 web service integration
13 web service integration
 
09 material design
09 material design09 material design
09 material design
 
07 intent
07 intent07 intent
07 intent
 
05 binding and action
05 binding and action05 binding and action
05 binding and action
 
04 layout design and basic widget
04 layout design and basic widget04 layout design and basic widget
04 layout design and basic widget
 
03 activity life cycle
03 activity life cycle03 activity life cycle
03 activity life cycle
 
02 create your first app
02 create your first app02 create your first app
02 create your first app
 
01 introduction
01 introduction 01 introduction
01 introduction
 
Material Theme
Material ThemeMaterial Theme
Material Theme
 
00 Android Wear Setup Emulator
00 Android Wear Setup Emulator00 Android Wear Setup Emulator
00 Android Wear Setup Emulator
 
MongoDB Replication Cluster
MongoDB Replication ClusterMongoDB Replication Cluster
MongoDB Replication Cluster
 
MongoDB Shard Cluster
MongoDB Shard ClusterMongoDB Shard Cluster
MongoDB Shard Cluster
 
IT Automation with Chef
IT Automation with ChefIT Automation with Chef
IT Automation with Chef
 
IT Automation with Puppet Enterprise
IT Automation with Puppet EnterpriseIT Automation with Puppet Enterprise
IT Automation with Puppet Enterprise
 
Using PhoneGap Command Line
Using PhoneGap Command LineUsing PhoneGap Command Line
Using PhoneGap Command Line
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | Workshop
 
OpenStack Cheat Sheet V2
OpenStack Cheat Sheet V2OpenStack Cheat Sheet V2
OpenStack Cheat Sheet V2
 
REST API with CakePHP
REST API with CakePHPREST API with CakePHP
REST API with CakePHP
 

Android App Development 04 : Location API

  • 1. 4 Location API Anuchit Chalothorn anoochit@gmail.com Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  • 2. Determine the current location Most Android devices allow to determine the current geolocation. This can be done via a GPS (Global Positioning System) module, via cell tower triangulation or via wifi networks. Ref: http://developer.android.com/guide/topics/location/index.html
  • 3. Location Manager The LocationManager class provides access to the Android location service. This services allows to access location providers, to register location update listeners and proximity alerts and more.
  • 4. LocationProvider The LocationProvider class is the superclass of the different location providers which deliver the information about the current location. This information is stored in the Location class.
  • 5. LocationProvider LocationProvider Description network Uses the mobile network or WI-Fi to determine the best location. Might have a higher precision in closed rooms then GPS. gps Use the GPS receiver in the Android device to determine the best location via satellites. Usually better precision than network. passive Allows to participate in location of updates of other components to save energy
  • 6. Selecting LocationProvider via Criteria For a flexible selection of the best location provider use a Criteria object, in which you can define how the provider should be selected.
  • 7. Permission If you want to access the GPS sensor, you need the ACCESS_FINE_LOCATION permission. Otherwise you need the ACCESS_COARSE_LOCATION permission.
  • 8. Prompt the user to Enabled GPS LocationManager service = (LocationManager) getSystemService (LOCATION_SERVICE); boolean enabled = service .isProviderEnabled(LocationManager.GPS_PROVIDER); if (!enabled) { Intent intent = new Intent(Settings. ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); }
  • 9.
  • 10. Workshop: Get current location Use criteria find the best provider then get current position from it. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider (criteria, true); Location location = locationManager.getLastKnownLocation (provider); float lat = (float) location.getLatitude(); float lon = (float) location.getLongitude();
  • 11.
  • 13. Google Maps Google provides via Google play a library for using Google Maps in your application. The following description is based on the Google Maps Android API v2 which provides significant improvements to the older API version. Ref: https://developers.google.com/maps/documentation/android/
  • 14. Getting the Google Map key To use Google Maps you need to create a valid Google Maps API key. The key is free, you can use it with any of your applications that call the Maps API, and it supports an unlimited number of users.
  • 15.
  • 16.
  • 17.
  • 18. SHA-1 for your signature key keytool -list -v -alias androiddebugkey - keystore ~/.android/debug.keystore - storepass android -keypass android
  • 19.
  • 20.
  • 21. Work with Google Map ● Register your app and sha1 for publish keystore and debug keystore ● Import Google Play Service library ● Add some permission and information ● Add MapView to layout ● then coding
  • 22. MapView The library provides the com.google.android. gms.maps.MapFragment class and the MapView class for displaying the map component. <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment" />
  • 25. Map & Marker static final LatLng SIPA = new LatLng(13.885245,100.565586); GoogleMap map = ((MapFragment) getFragmentManager(). findFragmentById(R.id.map)).getMap(); if (map!=null) { Marker sipa = map.addMarker(new MarkerOptions().position(SIPA).title("SIPA")); }
  • 26.
  • 27. End