1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
| package com.android.map;
import java.util.ArrayList;
import java.util.List;
import java.lang.*;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import java.util.List;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import com.android.map.MaBaseSQLite;
import com.android.map.PositionBDD;
import com.android.map.Position;
import com.android.map.PositionMarkerList;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class HelloGoogleMapActivity extends MapActivity {
private static final String NOM_BDD = null;
private static final int VERSION_BDD = 0;
private MapView mapView;// Vue qui affiche la carte
private MapController mc; // Controlleur pour la carte
private TextView textView;
private double lat = 0;
private double lng = 0;
private LocationManager lm;
private GeoPoint p; // Point a afficher sur la carte
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
//bouton de zoom
mapView = (MapView) this.findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
//niveau de zoom
mc = mapView.getController();
//Création d'une instance de ma classe PositionBDD
MaBaseSQLite posBdd = new MaBaseSQLite(this, NOM_BDD, null, VERSION_BDD);
//On ouvre la base de données pour écrire dedans
posBdd.open();
//Création d'une position
for(int i=0;i<8;i++){
Position position = new Position(36.854128,10.33468);
//On insère la position que l'on vient de créer
posBdd.insertPosition(position);
}
MapOverlay mapOverlay = new MapOverlay();
// Affiche l'ensemble des marqueurs enregistrés en DB
PositionMarkerList positionMarkerList = new PositionMarkerList(this.getResources().getDrawable(R.drawable.pink), this);
List<Overlay> mapOverlays = mapView.getOverlays();
mapOverlays.add(positionMarkerList);
// Récupère de la DB les marqueurs enregistrés
// et ajoute les à la carte
for (Position positionToAdd : posBdd.getPositionWithId()) {
addPosition(positionToAdd);
}
}catch(Exception e){
Log.e("mon erreur",e.getMessage());
}
/*
//**classe definie pour ajouter un markeur
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//translater le GeoPoint au pixel
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//ajouter un marqueur
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pink);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//quand l'utilisateur lache son doigt
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(), "Toast.makeText(this, positionFromBdd.toString(), Toast.LENGTH_LONG).show();", Toast.LENGTH_LONG).show();
}
return false;
}
} */
/*double lat = position.getLatitude();
double lng = position.getLongitude();
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
//bouton de zoom
mapView = (MapView) this.findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
//niveau de zoom
mc = mapView.getController();
//double lat []={36.854119,36.857776,36.858103,36.856171,36.854025,36.853424,36.852471,36.857587};
// double lng []={10.334905,10.329466,10.331537,10.314864,10.323157,10.324284,10.323694,10.318501};
// String com []={"Therme Anthonin","Amphitheatre","Villas Romaines","Amphitheatre","Acropolium","Musée national","Byrsa","Phenix Carthage"};
/* lat = 36.854119;
lng = 10.334905;
for(int i=0;i<8;i++){
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setCenter(p);
mc.setZoom(21);
//ajouter un marqueur
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays(); // Ajouter une couche à la vue
listOfOverlays.clear();
listOfOverlays.add(mapOverlay); // met à jour l'affichage
mapView.invalidate();*/
}
//changer le type de la carte
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//vue par satellite
if (keyCode == KeyEvent.KEYCODE_S)
{
mapView.setSatellite(!mapView.isSatellite());
return true;
}
//vue des Street
if (keyCode == KeyEvent.KEYCODE_S)
{
mapView.setStreetView(!mapView.isSatellite());
return true;
}
return super.onKeyDown(keyCode, event);
}
public class MaBaseSQLite extends SQLiteOpenHelper{
public static final String NOM_BDD = "position.db";
private static final String TABLE_POS = "table_Position";
private static final String COL_ID = "ID";
private static final String COL_LONGITUDE = "longitude";
private static final String COL_LATITUDE = "latitude";
private static final String CREATE_BDD = "CREATE TABLE " +TABLE_POS + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONGITUDE + " TEXT NOT NULL, "
+ COL_LATITUDE + " TEXT NOT NULL);";
public MaBaseSQLite(Context context, String NOM_BDD, CursorFactory factory, int VERSION_BDD) {
super(context, NOM_BDD, null, VERSION_BDD);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//on a créé la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_BDD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE " + TABLE_POS + ";");
onCreate(db);
}
private static final int VERSION_BDD = 1;
// private static final String TABLE_POS = "table_Position";
// private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
//private static final String COL_LONGITUDE = "longitude";
private static final int NUM_COL_LONGITUDE = 1;
//private static final String COL_LATITUDE = "latitude";
private static final int NUM_COL_LATITUDE = 2;
private SQLiteDatabase bdd;
private MaBaseSQLite maBaseSQLite;
/*public maBaseSQLite(Context context){
//On créer la BDD et sa table
maBaseSQLite = new MaBaseSQLite(context, NOM_BDD, null, VERSION_BDD);
}*/
public void open(){
//on ouvre la BDD en écriture
bdd = maBaseSQLite.getWritableDatabase();
}
public void close(){
//on ferme l'accès à la BDD
bdd.close();
}
public SQLiteDatabase getBDD(){
return bdd;
}
/**
* Enregistre la position donnée.
* */
public long insertPosition(Position position){
if (null != position) {
//Création d'un ContentValues
ContentValues values = new ContentValues();
//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(COL_LONGITUDE, position.getLongitude());
values.put(COL_LATITUDE, position.getLatitude());
//on insère l'objet dans la BDD via le ContentValues
bdd.close();
return bdd.insert(TABLE_POS, null, values);
} else {
return 0;
} // end-if
}
/**
* Récupère l'ensemble des positions disponibles en DB.
*/
public ArrayList<Position> getPositionWithId(){
ArrayList<Position> output = new ArrayList<Position>();
String[] colonnesARecup = new String[] {
COL_LONGITUDE,
COL_LATITUDE
};
Cursor cursorResults = bdd.query(TABLE_POS, colonnesARecup, null, null, null,null,null);
if (null != cursorResults) {
if (cursorResults.moveToFirst()) {
do {
double longitude = cursorResults.getDouble(cursorResults.getColumnIndex(COL_LONGITUDE));
double latitude = cursorResults.getDouble(cursorResults.getColumnIndex(COL_LATITUDE));
Position positionToAdd = new Position(longitude,latitude);
output.add(positionToAdd);
} while (cursorResults.moveToNext());
} // end-if
} // end-if
bdd.close();
return output;
}
}
/*
public class PositionBDD {
private static final int VERSION_BDD = 1;
private static final String TABLE_POS = "table_Position";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_LONGITUDE = "longitude";
private static final int NUM_COL_LONGITUDE = 1;
private static final String COL_LATITUDE = "latitude";
private static final int NUM_COL_LATITUDE = 2;
private SQLiteDatabase bdd;
private MaBaseSQLite maBaseSQLite;
public PositionBDD(Context context){
//On créer la BDD et sa table
maBaseSQLite = new MaBaseSQLite(context, NOM_BDD, null, VERSION_BDD);
}
public void open(){
//on ouvre la BDD en écriture
bdd = maBaseSQLite.getWritableDatabase();
}
public void close(){
//on ferme l'accès à la BDD
bdd.close();
}
public SQLiteDatabase getBDD(){
return bdd;
}
//Enregistre la position donnée.
public long insertPosition(Position position){
if (null != position) {
//Création d'un ContentValues
ContentValues values = new ContentValues();
//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(COL_LONGITUDE, position.getLongitude());
values.put(COL_LATITUDE, position.getLatitude());
//on insère l'objet dans la BDD via le ContentValues
bdd.close();
return bdd.insert(TABLE_POS, null, values);
} else {
return 0;
} // end-if
}
//Récupère l'ensemble des positions disponibles en DB.
public ArrayList<Position> getPositionWithId(){
ArrayList<Position> output = new ArrayList<Position>();
String[] colonnesARecup = new String[] {
COL_LONGITUDE,
COL_LATITUDE
};
Cursor cursorResults = bdd.query(TABLE_POS, colonnesARecup, null, null, null,null,null);
if (null != cursorResults) {
if (cursorResults.moveToFirst()) {
do {
double longitude = cursorResults.getDouble(cursorResults.getColumnIndex(COL_LONGITUDE));
double latitude = cursorResults.getDouble(cursorResults.getColumnIndex(COL_LATITUDE));
Position positionToAdd = new Position(longitude,latitude);
output.add(positionToAdd);
} while (cursorResults.moveToNext());
} // end-if
} // end-if
bdd.close();
return output;
}
/*public Position getPositionWithId(){
//Récupère dans un Cursor les valeur correspondant à une position contenu dans la BDD (ici on sélectionne la position grâce à son id)
Cursor c = bdd.query(TABLE_POS, new String[] { COL_LONGITUDE, COL_LATITUDE}, null, null, null,null,null);
return cursorToPosition(c);
}
//Cette méthode permet de convertir un cursor en une position
private Position cursorToPosition(Cursor c){
//si aucun élément n'a été retourné dans la requête, on renvoie null
if (c.getCount() == 0)
return null;
//Sinon on se place sur le premier élément
c.moveToFirst();
//On a créé une position
Position position = new Position();
//on l'affecte toutes les infos grâce aux infos contenues dans le Cursor
//position.setId(c.getInt(NUM_COL_ID));
double longitude = c.getDouble(c.getColumnIndex(COLUMN_LONGITUDE));
double latitude = c.getDouble(c.getColumnIndex(COLUMN_LATITUDE));
position.setLongitude(c.getFloat(NUM_COL_LONGITUDE));
position.setLatitude(c.getFloat(NUM_COL_LATITUDE));
//On ferme le cursor
c.close();
//On retourne la position
return position;
}
}*/
public class Position {
private int id;
private double longitude;
private double latitude;
public Position(){}
public Position(double longitude, double latitude){
//this.id=id;
this.longitude = longitude;
this.latitude = latitude;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public String toString(){
//return "ID : "+id+"\nLONGITUDE : "+longitude+"\nLATITUDE : "+latitude;
String output = "\nlongitude = " + longitude +"\nlatitude = " + latitude ;
return output;
}
}
public class PositionMarkerList extends ItemizedOverlay {
/**
* Contient la liste des marqueurs à afficher.
*/
private ArrayList<OverlayItem> marqueurs = new ArrayList<OverlayItem>();
/**
* Référence vers le contexte d'exécution.
*/
private Context context;
public PositionMarkerList(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
//this.context = context;
// TODO Auto-generated constructor stub
}
/**
* Ajoute un marqueur à afficher.
*/
public void addMarker(OverlayItem overlay) {
marqueurs.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return marqueurs.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return marqueurs.size();
}
/**
* Affiche un dialogue décrivant le marqueur lorsque l'utilisateur clique sur un marqueur.
*/
@Override
protected boolean onTap(int index) {
OverlayItem item = marqueurs.get(index);
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
private void addPosition(Position position) {
double lat = position.getLatitude();
double lng = position.getLongitude();
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setCenter(p);
mc.setZoom(17);
Toast.makeText(getBaseContext(),
"Location change to : Latitude = " + lat + " Longitude = " + lng,
Toast.LENGTH_SHORT).show();
OverlayItem overlayitem = new OverlayItem(p,"hello", position.toString());
positionMarkerList.addMarker(overlayitem);
}
PositionMarkerList positionMarkerList = null;
@Override
protected boolean isRouteDisplayed()
{
return false;
}
} |
Partager