Localisation
Localisation
Localisation
On demande de créer la table position ci-dessous dans la base de données localisation sous
MySQL :
--
-- Structure de la table `position`
--
La classe Position :
<?php
class Position {
private $id;
private $latitude;
Page 1 sur 9
M. LACHGAR
private $longitude;
private $date;
private $imei;
function getId() {
return $this->id;
}
function getLatitude() {
return $this->latitude;
}
function getLongitude() {
return $this->longitude;
}
function getDate() {
return $this->date;
}
function setId($id) {
$this->id = $id;
}
function setLatitude($latitude) {
$this->latitude = $latitude;
}
function setLongitude($longitude) {
$this->longitude = $longitude;
}
function setDate($date) {
$this->date = $date;
}
function getImei() {
return $this->imei;
}
function setImei($imei) {
$this->imei = $imei;
}
La classe Connexion :
<?php
class Connexion {
private $connextion;
Page 2 sur 9
M. LACHGAR
$this->connextion = new PDO("mysql:host=$host;dbname=$dbname", $login, $password);
$this->connextion->query("SET NAMES UTF8");
} catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
}
function getConnextion() {
return $this->connextion;
}
?>
L’interface IDao :
<?php
interface IDao {
La classe PositionService :
<?php
include_once 'dao/IDao.php';
include_once 'classe/Position.php';
include_once 'connexion/Connexion.php';
Page 3 sur 9
M. LACHGAR
public function getById($obj) {
Script createPosition :
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
include_once 'service/PositionService.php';
create();
}
Les permissions
Rajouter les permissions suivantes dans le fichier de configuration de votre projet
« AndroidManifest.xml » :
Page 4 sur 9
M. LACHGAR
Récupération des cordonnées avec GPS
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = Volley.newRequestQueue(getApplicationContext());
addPosition(latitude, longitude);
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
String newStatus = "";
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
newStatus = "OUT_OF_SERVICE";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
newStatus = "TEMPORARILY_UNAVAILABLE";
break;
case LocationProvider.AVAILABLE:
newStatus = "AVAILABLE";
break;
}
String msg = String.format(getResources().getString(R.string.provider_new_status),
provider, newStatus);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
@Override
public void onProviderEnabled(String provider) {
String msg = String.format(getResources().getString(R.string.provider_enabled),
provider);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
String msg = String.format(getResources().getString(R.string.provider_disabled),
provider);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
});
Page 5 sur 9
M. LACHGAR
}
void addPosition(final double lat, final double lon) {
StringRequest request = new StringRequest(Request.Method.POST,
insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
TelephonyManager telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
return params;
}
};
requestQueue.add(request);
}
Exemple d’insertion :
Page 6 sur 9
M. LACHGAR
TP : Google Map
Objectifs :
Enoncé :
1. Dans le projet précédant créer une activité de type « Google Map Activity ».
2. Récupérer la clé « google_maps_key », pour ce faire, vous pouvez suivre les étapes dans le
fichier xml « google_maps_api.xml ».
if ($_SERVER["REQUEST_METHOD"] == "POST") {
include_once 'service/PositionService.php';
showPositions();
}
function showPositions() {
$cs = new PositionService();
header('Content-type: application/json');
echo json_encode(array("positions" => $cs->getAll()));
}
Page 7 sur 9
M. LACHGAR
5. Dans l’activité « Main » ajouter un bouton pour naviguer vers l’activité Map.
Page 8 sur 9
M. LACHGAR
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
Page 9 sur 9
M. LACHGAR