TP
: GPS et Map
1. Créer un nouveau projet Android
2. Choisir « Google Maps Activity »
3. Cliquer sur Next ensuite Finish.
Page 1 sur 4
4. Structure de projet créé
5. Récupérer la clé de Google Maps API.
a. Dans le fichier google_maps_api.xml récupérer le lien permettant de générer la
clé.
b. Après la génération de la clé initialiser la valeur de la constante google_maps_key
par la clé récupérée.
6. Lancer l’application pour afficher la Map.
Page 2 sur 4
7. Ajouter les permissions suivantes :
<uses-permission
android:name="[Link].ACCESS_FINE_LOCATION" />
<uses-permission android:name="[Link]" />
8. Ajouter la méthode suivant de la classe Activity :
private void buildAlertMessageNoGps() {
final [Link] builder = new [Link](this);
[Link]("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new [Link]() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new
Intent([Link].ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new [Link]() {
public void onClick(final DialogInterface dialog, final int id) {
[Link]();
}
});
final AlertDialog alert = [Link]();
Page 3 sur 4
[Link]();
}
9. Modifier le corps de la méthode onMapReady comme suit :
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
[Link](new MarkerOptions().position(sydney).title("Marker in Sydney"));
[Link](getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
boolean permissionGranted = [Link](this,
[Link].ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
if (permissionGranted) {
[Link](LocationManager.NETWORK_PROVIDER, 1000, 50, new
LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude = [Link]();
double longitude = [Link]();
[Link](getApplicationContext(), latitude+" "+longitude, Toast.LENGTH_SHORT).show();
[Link](new MarkerOptions().position(new LatLng(latitude,
longitude)).title("Marker"));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
@Override
public void onProviderEnabled(String provider) {
@Override
public void onProviderDisabled(String provider) {
buildAlertMessageNoGps();
}
});
} else {
[Link](this, new String[]{[Link].ACCESS_FINE_LOCATION},
200);
}
[Link]([Link](sydney));
}
10. Zoomer sur une position dans la Map :
float zoomLevel = 15.0f; //This goes up to 21
[Link]([Link](position, zoomLevel));
Page 4 sur 4