lab 3: google maps api - matteo...
TRANSCRIPT
![Page 1: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/1.jpg)
1
Matteo Camilli
[email protected]://camilli.di.unimi.it
a.a. 2014/15
Lab 3: Google Maps API
Università degli Studi di Bergamo Dipartimento di Ingegneria
![Page 2: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/2.jpg)
Google Maps Android API v2
2
• Getting started:• https://developers.google.com/maps/documentation/android/
start#getting_the_google_maps_android_api_v2
![Page 3: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/3.jpg)
Importare progetto in eclipse
3
• Per poter utilizzare le Google Maps API v2 occorre:• Google Play Service Library• Android Support Library
Android����������� ������������������ SDK����������� ������������������ manager
![Page 4: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/4.jpg)
Android Support Library
4
• Si trova nella dir:• <adt-sdk-folder>/sdk/extras/android/support/v13/android-support-v13.jar
copiare����������� ������������������ jar����������� ������������������ nella����������� ������������������ dir����������� ������������������ /libs
![Page 5: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/5.jpg)
Google Play Service Library
5
• Select New > Android > Android Project from Existing Code and click Next.• Select Browse..., enter
<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib, and click Finish.
controllare����������� ������������������ che����������� ������������������ sia����������� ������������������ selezionato
![Page 6: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/6.jpg)
6
Google Play Service Library
• Importare google-play-services_lib project come libreria
aggiungere����������� ������������������ tramite����������� ������������������ add
![Page 7: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/7.jpg)
Android certificate and the Google Maps API key
7
1) Recuperare SHA-1 fingerprint sul certificato della vostra App.2) Registrare un progetto nella Google APIs Console e aggiungere le Maps API
come service per il progetto.3) Richiedere una o più chiavi.4) Aggiungere la chiave nel Manifest dell’App.
• https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key
![Page 8: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/8.jpg)
Android certificate
8
• Ogni App Android, per essere installata su dispositivo o emulatore deve avere associato un certificato digitale che identifica lo sviluppatore.
• Quando sviluppate un’App con eclipse viene associato un certificato di Debug.• Il certificato di Debug si chiama debug.keystore, e viene creato di default nella
directory:• OS X and Linux: ~/.android/• Windows Vista and Windows 7: C:\Users\your_user_name\.android\
![Page 9: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/9.jpg)
9
Android certificate
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
• List the SHA-1 fingerprint:
Alias name: androiddebugkey Creation date: Jan 01, 2013 Entry type: PrivateKeyEntry Certificate chain length: 1 Certificate[1]: Owner: CN=Android Debug, O=Android, C=US Issuer: CN=Android Debug, O=Android, C=US Serial number: 4aa9b300 Valid from: Mon Jan 01 08:04:04 UTC 2013 until: Mon Jan 01 18:04:04 PST 2033 Certificate fingerprints: MD5: AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6A:AC:F9 SHA1: BB:0D:AC:74:D3:21:E1:43:07:71:9B:62:90:AF:A1:66:6E:44:5D:75 Signature algorithm name: SHA1withRSA Version: 3
• Risultato:
![Page 10: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/10.jpg)
Create an API project in the Google APIs Console
10
1) Andare all’indirizzo: https://console.developers.google.com2) Creare un API Project3) Selezionare APIs & auth > APIs nel menu di sinistra4) Impostare ON su Google Maps Android API v2
![Page 11: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/11.jpg)
11
Create an API project in the Google APIs Console
5) Selezionare Credentials nel menu di sinistra e registrate la vostra app selezionando Create new key > Android key. Inserire SHA1 fingerprint e package name dell’App.
![Page 12: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/12.jpg)
12
6) La console vi risponde con la chiave associata alla vostra app, es:
AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
7) Nel Manifest della vostra app, come figlio dell’elemento <application> inserire:
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="API_KEY"/>
8) Sostituire API_KEY con la vostra chiave.
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0"/>
Create an API project in the Google APIs Console
![Page 13: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/13.jpg)
13
• REpresentational State Transfer• Insieme di principi di architetture di rete, i quali delineano come le risorse sono definite
e indirizzate.Utilizza i metodi HTTP (GET, POST, PUT, DELETE) per eseguire una serie di operazioni e consentire l’interazione client-server.
• Un API REST è un insieme di operazioni che possono essere invocate utilizzando l’URI richiamato come parametro/i per la specifica operazione. • ESEMPIO: se vogliamo un servizio che ci fornisca la lista degli utenti del sistema,
possiamo fare HTTP GET dell’URI miaApp/users/all, Il parametro ‘all’ dice alla nostra applicazione che deve fornire la lista di tutti gli utenti.
• ESEMPIO: Servizio che fornisce i dati sulle stazioni del servizio di bike sharing bikeMi: http://api.citybik.es/bikemi.json
• Libreria Java per utilizzare servizi REST (anche su Android)• RETROFIT http://square.github.io/retrofit/
REST Services
![Page 14: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/14.jpg)
14
JSON
• JavaScript Object Notation, è un formato adatto per lo scambio dei dati in applicazioni client-server.
• I tipi di dati supportati da questo formato sono:• booleani (true e false);• interi, reali, virgola mobile;• stringhe racchiuse da doppi apici ( " );• array (sequenze ordinate di valori, separati da virgole e racchiusi in parentesi quadre [ ] );• array associativi (sequenze coppie chiave-valore separate da virgole racchiuse in parentesi graffe);• null.
• Esempio:{ "type": “menu", "value": "File", "items": [ {"value": "New", "action": "CreateNewDoc"}, {"value": "Open", "action": "OpenDoc"}, {"value": "Close", "action": “CloseDoc"} ] }
![Page 15: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/15.jpg)
15
JSON
• Generate Plain Old Java Objects from JSON:• http://www.jsonschema2pojo.org/
• Tramite la libreria Retrofit, possiamo eseguire richieste al server per ricevere i dati in formato JSON e convertirli automaticamente in formato POJO.
![Page 16: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/16.jpg)
Esempio: bikeMe
16
![Page 17: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/17.jpg)
Esempio: bikeMe
17
Modalità “a piedi” Modalità “in bici” vista satellite
![Page 18: Lab 3: Google Maps API - Matteo Camillicamilli.di.unimi.it/main/teaching_files/unibg/lab3-maps.pdf · 2015-01-07 · Android certificate and the Google Maps API key 7 1) Recuperare](https://reader033.vdocuments.site/reader033/viewer/2022042222/5ec8f022df50fc74fd10e885/html5/thumbnails/18.jpg)
Esercizio
18
1. Partendo dall’applicazione SimpleMap, visualizzare sulla mappa la posizione di tutte le stazioni del bikeMi.
2. Al tap su una stazione visualizzare le informazioni su:• nome stazione, numero stalli, numero bici
3. Visualizzare i marker con colori diversi a seconda del numero di bici disponibili:• 0 bici: Rosso• Tra 1 e 3 bici: Giallo• Maggiore di 3 bici: Verde
4. Creare un pulsante per eseguire il refresh delle stazioni5.Creare un pulsante per eseguire il cambio di modalità:• Modalità a piedi: come prima• Modalità in bici: i colori delle stazioni cambiano in base agli stalli disponibili (non più
in base alle bici disponibili)