Is the sequence of items is always the same in MapOverlay? - android

Firstly I add markers to the overlay:
private MapOverlay itemizedOverlay;
Cursor items = mDbHelper.fetchAllItems();
startManagingCursor(items);
for (int i = 0; i < items.getCount(); i++) {
items.moveToPosition(i);
OverlayItem overlayItem = new OverlayItem(markerPoint, "", "");
itemizedOverlay.addOverlay(overlayItem);
}
mapOverlays.add(itemizedOverlay);
Now I need to update markers (change drawable).
Can I do:
Cursor items = mDbHelper.fetchAllItems();
startManagingCursor(items);
for (int i = 0; i < items.getCount(); i++) {
items.moveToPosition(i);
itemizedOverlay.getItem(i).setMarker();
}
mapOverlays.add(itemizedOverlay);
Will itemizedOverlay.getItem(i) always return items in the same sequence?
Records in the database are not added/deleted.

Will itemizedOverlay.getItem(i) always return items in the same sequence?
That is up to you. You are the one implementing getItem().

Related

Add polyline to google maps with different colors android

I'm trying to get coordinates from Firebase to create polyline on Google maps with different colors to each user. The problem is when I get this data and insert it on google maps it shows with the same color.
List<Integer> colors = new ArrayList<>();
colors.add(-9784993);
colors.add(-6501807);
for (int a = 0; a <= userList.size()-1; i++) {
for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
dataClass d = objSnapshot.getValue(dataClass.class);
LatLng start = new LatLng(d.getLat(), d.getLongi());
elasticList.add(start);
polylineOptionsTest[a] = new PolylineOptions()
.addAll(elasticList)
.color(colors.get(a)) //Get color from list called "colors"
.clickable(true);
polyline2 = mMap.addPolyline(polylineOptionsTest[a]);
}
}
Probably that is because userList.size() is equals 1 and colors.get(a) always returns colors[0] (-9784993) color. In other words you add polylines for single user.
Update: mistake is in for loop - you should use:
for (int a = 0; a <= userList.size()-1; a++) {
instead of
for (int a = 0; a <= userList.size()-1; i++) {
you increase i variable, not a.

Converting optionMarker array into Marker array

In an android application I want to check if a targetMarker is among the friendMarkers and if it is, then change that one friendMarker to the targetMarker, thus I don't have to remove the friendMarker and add the targetMarker. My question is how do I do this?
First, I need to convert the optionMarker array into a Marker array.
Second, I need to check the Id-s
Third, I need to transform the friendMarker into a targetMarker
MainActivity - I activate the method in the actionbar: (the storeMarker is not fully functioning, tries to store the optionMarker array in a Marker array - unsuccessfully.
if (id == R.id.get_target) {
// this is for getting the random target - and placing a marker on the Map
targetClass = randomTarget.getRandomTarget();
targetMarker = googleMap.addMarker(targetClass.marker);
// here I get the friendMarkers
MarkerOptions[] markers = randomTarget.getMarkers();
Marker [] array;
// the rest of the code is my try to convert the optionMarker array into Marker array
for (int j = 0; j < markers.length; j++){
// array[j] = storeMarkers().getId();
}
if (targetMarker.getId() == storeMarkers().getId()) {
}
}
And this is from the Target class - the optionMarker array
// puts the online player's location into an array of markeroptions
public MarkerOptions [] getMarkers() {
playerFunctions = new PlayerFunctions();
JSONArray array = playerFunctions.getAllOnline();
try {
MarkerOptions[] markers = new MarkerOptions[array.length()];
for(int i = 0; i < array.length(); i++) {
JSONObject element = array.getJSONObject(i);
markers[i] = new MarkerOptions()
.position(new LatLng(Double.parseDouble((String)element.get("lat")), Double.parseDouble((String)element.get("long"))))
.title((String)element.get("name"));
}
return markers;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
Thanks for the replies.

Android GoogleMaps v2: error when adding multiple markers using 'for' loop

I need to add a given number of markers to a map using a for loop. The Log messages tell me the function to add each marker is called, but only one marker (two at most) are displayed on the map. My two functions are the following:
private void paintInMap(String description){
map.clear(); // to erase previous markers
String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
String[] route = description.split(", "); // split the different places of the route description
for(int i=0; i<route.length; i++){
for(int j=0; j<zones.length; j++{
if(route[i].equals(zones[j])){
LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
placeMarker(latLng, zones[j]);
}
}
}
}
and:
private void placeMarker(LatLng coordinates, String name){
map.addMarker(new MarkerOptions()
.title(name)
.icon(BitMapDescriptorFactory.fromResource(R.drawable.gpsmap))
.position(coordinates)
.flat(true)
.rotation(90));
Log.d("PLACE", name+" added to map");
}
Apparently my code is correct, but on runtime it only displays one (or two) markers. I have checked the Log messages and the function is being called, but the markers do no appear. Moreover, one of the markers appears in an unspecified location (which corresponds to the first value of the coordinates array by the way)
Is this a runtime bug in Eclipse? How can I solve this?
map.clear(); // to erase previous markers
new AsyncTask<String, MarkerOptions, Void>() {
private void placeMarker(LatLng coordinates, String name) {
publishProgress(new MarkerOptions()
.title(name)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.gpsmap))
.position(coordinates)
.flat(true)
.rotation(90));
Log.d("PLACE", name + " added to map");
}
#Override
protected Void doInBackground(String... params) {
String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
String[] route = params[0].split(", "); // split the different places of the route description
for (int i=0; i<route.length; i++) {
for (int j=0; j<zones.length; j++) {
if (route[i].equals(zones[j])) {
LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
placeMarker(latLng, zones[j]);
}
}
}
return null;
}
#Override
protected void onProgressUpdate(MarkerOptions... markers) {
map.addMarker(markers[0]);
}
}.execute(description);
I finally solved it by running the for loop in the UI thread instead of doing it using an AsyncTask
......
route = descriptionRoute.split(", ");
coordinates = getCoordinates(coord);
String[] zonas = getResources().getStringArray(R.array.array_zonas_madrid);
String[] coord = getResources().getStringArray(R.array.array_coordinates);
for(int i=0; i<route.length; i++){
for(int j=0; j<zonas.length; j++){
if(route[i].equals(zonas[j])){
LatLng latLng = getCoordinates(coord[j]);
placeMarker(latLng, zonas[j]);
}
}
}
....

for-loop query all rows in database. but result just show the last of row

I have 4 rows of latitude in database and I want to get all that rows of it. In CafeDataSource I had query it and set it to ArrayList<HashMap<String, Object>>.
When I use it in TopActivity in forloop, I can query all 4 rows. but all 4 rows just have one value of the last row. Ex, my database (1,2,3,4) but my result (4,4,4,4).
I had log(db) in getArrCursor() for value and it works fine.
I had log(number) in for loop for value but it shows me all and all are the last row.
How can I query it all and different row?
CafeDataSource
public ArrayList<HashMap<String, Object>> getArrCursor(){
arrCursor = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> map;
Cursor cursor = database.rawQuery("SELECT * FROM "+CafeDbOpenHelper.TABLE_CAFE, null);
if(cursor != null){
while(cursor.moveToNext()){
map = new HashMap<String, Object>();
map.put(CafeDbOpenHelper.CAFE_TITLE, cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_TITLE)));
map.put(CafeDbOpenHelper.CAFE_THUMB, cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_THUMB)));
map.put(CafeDbOpenHelper.CAFE_LATITUDE, cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LATITUDE)));
map.put(CafeDbOpenHelper.CAFE_LONGITUDE, cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LONGITUDE)));
Log.i("db", "" +cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LATITUDE)));
arrCursor.add(map);
}
}
return arrCursor;
}
TopActivity
int position = arrCursor.size();
for (int i = 0; i < position; i++) {
Log.i("number", "" +arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_LATITUDE));
double lat = (Double) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_LATITUDE);
double lng = (Double) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_LONGITUDE);
LatLng latlong = new LatLng(lat, lng);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker maker = map.addMarker(new MarkerOptions().position(latlong).title((String) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_TITLE)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 17));
LinearLayout includeMap = (LinearLayout) findViewById(R.id.lin_map);
includeMap.setVisibility(v.VISIBLE);
}
Position never changes but you are using that to get from the Array every time. Try instead
double lat = (Double) arrCursor.get(i).get(CafeDbOpenHelper.CAFE_LATITUDE);
double lng = (Double) arrCursor.get(i).get(CafeDbOpenHelper.CAFE_LONGITUDE);
Edit
Did you change this line
Marker maker = map.addMarker(new MarkerOptions().position(latlong).title((String) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_TITLE)));
to
Marker maker = map.addMarker(new MarkerOptions().position(latlong).title((String)
arrCursor.get(i).get(CafeDbOpenHelper.CAFE_TITLE)));
You are starting at the i position of your Array so every time you want to access that postion you will use i

Add multiple geopoints and markers automatically in Google Maps

Is it possible to make the addition of a geopoint in a map more "automatic"? I mean, if we have many points to add in the map (more than 100), we don't have to add them one by one like that:
GeoPoint point2 = new GeoPoint(microdegrees(36.86774),microdegrees(10.305302));
GeoPoint point3 = new GeoPoint(microdegrees(36.87154),microdegrees(10.341815));
GeoPoint point4 = new GeoPoint(microdegrees(36.876093),microdegrees(10.325716));
pinOverlay.addPoint(point2);
pinOverlay.addPoint(point3);
pinOverlay.addPoint(point4);
Is there a method to stock them all in a table and then the compiler adds them one by one?
You'll want to store them in either a SQLite database or some other form of data storage, and then you can pull them in and place them on the map with an ItemizedOverlay, see Google Map View (a tutorial).
Create your array from a database cursor
Cursor cursor = mDbHelper.getItems();
cursor.moveToFirst();
List<CatchItem> catchList = new ArrayList<CatchItem>();
if (cursor != null && cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
CatchItem item = new CatchItem();
item.Latitude = cursor.getDouble(cursor.getColumnIndex("latitude"));
item.Longitude = cursor.getDouble(cursor.getColumnIndex("longitude"));
catchList.add(item);
cursor.moveToNext();
}
}
ItemizedOverlay
List<Overlay> overlays = mMaps.getOverlays();
overlays.clear();
CatchesItemizedOverlay catchOverlays = new CatchesItemizedOverlay(getResources().getDrawable(R.drawable.map_markeroverlay_blue72), this);
for (int i = 0; i < catchList.size(); i++) {
double lat = catchList.get(i).Latitude;
double lng = catchList.get(i).Longitude;
GeoPoint geopoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
catchOverlays.addOverlay(new CatchOverlayItem(this, geopoint, catchList.get(i)));
}
overlays.add(catchOverlays);
CatchesItemizedOverlay is my own extended ItemizedOverlay (I needed custom functionality). The catchList object is just a custom object that has latitude and longitude.
Hopefully this works for you.
You could store them in a SQLite databse, and pull them as needed

Categories

Resources