mercredi 11 février 2015

How do I give a name and a different image on Google Maps Marker API that uses SQLite?

I have difficulties when they want to give a different name and a different marker images on Google Maps Marker, I use SQLite database to store the data from the marker, the following code from my project :


MaBase.java



public class MaBase extends SQLiteOpenHelper {

private static final String TABLE_MARK ="marqueur.db";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";


private static final String CREATE_BDD = "CREATE TABLE " + TABLE_MARK + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONG + " TEXT NOT NULL, " +COL_LAT+" TEXT NOT NULL);";






public MaBase (Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
//on 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) {

db.execSQL("DROP TABLE " + TABLE_MARK + ";");
onCreate(db);
}

}


MainActivity.java



public class MainActivity extends Activity {

private static final String NOM_BDD = "marqueur.db";

private static final String TABLE_GEOPOINT = "geopoint";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";


static final LatLng TUNIS = new LatLng(36.894883, 10.1432776); // http://ift.tt/1zLNt69
private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// fct pour remplire la base avec qlq points pour le demo :)
sauver_point();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();

MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
Cursor c = db.query(TABLE_GEOPOINT, new String[] { COL_ID, COL_LONG,
COL_LAT }, null, null, null, null, null, null);

int col = c.getCount(); // col=0 pas de enregistrement qui verifie la
// condition
if (col == 0) {
Toast.makeText(MainActivity.this, "Pas de donnees ",
Toast.LENGTH_LONG).show();
// effacer le contenue champ login et mot de passe

} else {
c.moveToFirst();
while (c.isAfterLast() == false) {
// conversion int to string casting
String id = "" + c.getInt(0);
String longitude = c.getString(1);
String latitude = c.getString(2);
Marker marqueur = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble(latitude),
Double.parseDouble(longitude)))
.title("Bonjour Tunis")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark2)));
c.moveToNext();
}
}
c.close();
db.close();


map.moveCamera(CameraUpdateFactory.newLatLngZoom(TUNIS, 12.0f));

// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

// pour ajouter des points ala base sqlite juste pour le demo :)

void sauver_point() {
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(COL_ID , "1");
values.put(COL_LAT, "36.830722");
values.put(COL_LONG, "10.165672");

db.insert(TABLE_GEOPOINT, null, values);

// creer un autre utilisateur

values = new ContentValues();
values.put(COL_LAT , "36.830922");
values.put(COL_LONG, "10.275572");
db.insert(TABLE_GEOPOINT, null, values);

values = new ContentValues();
values.put(COL_LAT, "36.930522");
values.put(COL_LONG, "10.385572");
db.insert(TABLE_GEOPOINT, null, values);
values = new ContentValues();

values.put(COL_LAT, "36.750422");
values.put(COL_LONG, "10.495572");
db.insert(TABLE_GEOPOINT, null, values);


values.put(COL_LAT, "36.936422");
values.put(COL_LONG, "11.495572");
db.insert(TABLE_GEOPOINT, null, values);

values.put(COL_LAT, "36.990422");
values.put(COL_LONG, "9.995572");
db.insert(TABLE_GEOPOINT, null, values);



db.close();

}

}


Then I would do different Marker calling on my other layout in the form of maps, but how do I enter data in the layout to display the marker that I insert in the database? The following layout will I enter along with the marker maps :


fragment_maps.xml



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv_distance_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_alignParentTop="true" />

<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>


MapsFragment.java



public class GamesFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_maps, container, false);

return rootView;
}
}


Please help, thank you :)


Aucun commentaire:

Enregistrer un commentaire