Android maps set multiple markers invisible - android

How can I hide multiple markers on Android Maps V2?
I have 10 markers. 5 a certain type of cinema and 5 another type.
If I use the below code only the last marker disappears.
Marker cinema1,cinema2;
cinema1 = gMap.addMarker......
cinema1 = gMap.addMarker......
cinema2 = gMap.addMarker......
cinema2 = gMap.addMarker......
and so on
On Click Event
cinema1.setVisible(false);
This only sets the last marker = to cinema1 to invisible (obviously) so the questions is how can I set all 5 markers invisible without declaring a Marker for each?
Thanks

You should create list with markers and manipulate with its help.
Marker dilers1,dilers2;
List<Marker> markers = new ArrayList<>();
dilers1 = mMap.addMarker(new MarkerOptions().position(new LatLng(55.755823,37.622315)).title("Melbourne"));
dilers2 = mMap.addMarker(new MarkerOptions().position(new LatLng(55.765823,37.612315)).title("Melbourne2"));
markers.add(dilers1);
markers.add(dilers2);
public void changer(){
for (Marker mkr: markers) {
mkr.setVisible(false);
}
}

You could just use an Hashmap if your types have an ID
Hashmap<Integer, Marker> markers = new Hashmap<Integer, Marker>()
markers.Add(cinema1.getID(), gMap.addMarker(...));
markers.Add(cinema2.getID(), gMap.addMarker(...));
markers.Add(cinema3.getID(), gMap.addMarker(...));

Related

google maps, add new markers and delete old markers android

in my application i get 25 markers from the webservice, and i add them to the map, then when user move the map i detect the center position and i get new 25 markers to add them in the map. the process must be:
1-get 25 markers from webservice
2-add the 25 markers to the map
3-when map move, detect the center position
4-get new 25 markers
5-add the new 25 markers to the map
6-delete the old 25 markers from the map
my problem is in number 6, how can i delete the 25 old markers after adding the 25 new markers.
i hope that i can find any helpful ideas, and thank you
#Barns thank you for your help, that loop doesn't work and i get this error java.util.HashMap$HashIterator.nextEntry, so i change it with this loop and finnaly it works
for (Iterator<Map.Entry<String, Marker>> iterator = markerList.entrySet().iterator(); iterator.hasNext();){
Map.Entry<String, Marker> entry = iterator.next();
//Log.e("key", entry.getKey()+"");
String bikeId = entry.getKey();
int i = SearchBike(bikeId);
//Log.e("i",i+"");
if (i == 0) {
Marker marker = entry.getValue();
marker.remove();
iterator.remove();
markerList.remove(bikeId);
}
}
for SearchBike(bikeId) i create it because i can't use arrayList.contain() because the result is an object not a string
private int SearchBike(String id){
int i = 0;
for (Bikes bike : arrayList){
if (bike.getId().equals(id)) {
i++;
}
}
return i;
}
so if i==0 that's mean the marker doesn't exist in the new list and should be deleted from the map
Create a HashMap class variable to hold information about the markers:
HashMap<Long, Marker> markerList = new HashMap<>();
(I assume bike.getId() returns a long type, but if you have used a different type you must change the definition of the HashMap and the following code to reflect that type.)
After the for (Bikes bike : arrayList) loop is run go through all the values in the markerList and remove the Markers that aren't in the arrayList.
private void addMarkers(){
//set Markers of bikes list
//First loop!!
for (Bikes bike : arrayList){
BitmapDescriptor pinMarker = null;
LatLng latLng = new LatLng(Double.parseDouble(bike.getLatitude()),Double.parseDouble(bike.getLongitude()));
switch (bike.getBreakdown()){
case "false":
pinMarker = pinWork;
break;
case "true":
pinMarker = pinMaintenance;
break;
}
Marker marker = VelosMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(pinMarker)
.zIndex(1));
if(!markerList.containsKey(bike.getId())){
Marker marker = map.addMarker(marker);
//Add the marker to the marker list
markerList.put(bike.getId(), marker);
HashMap<String,String>data=new HashMap<String,String>();
data.put("id",bike.getId());
data.put("imei",bike.getImei());
data.put("inUse",bike.getInUse());
data.put("breakdown",bike.getBreakdown());
marker.setTag(data);
}
}
// This will iterate through all the items in the new list ...
//...and remove the markers that are not found in the new list
// Second Loop!!
Iterator<Long> it = markerList.keySet().iterator();
while(it.hasNext();){
Long key = it.next();
if(!bikeContains(key)){
Marker marker = markerList.remove(key);
marker.remove();
}
}
}
private boolean bikeContains(Long id){
for (Bikes bike : arrayList){
Long bikeId = bike.getId();
if(bikeId == id){
return true;
}
}
return false;
}
EXPLANATION:
The first time through addMarkers():
The markerList is empty so in line if(!markerList.containsKey(bike.getId())){ a new entry will be added every pass through the first loop for (Bikes bike : arrayList){. And the Marker will be added to the map. In the second loop for (Long key : markerList.keySet()){ we iterate through the HashMap and if the arrayList does not contain the "key" then it is removed from the markerList and from the map. But, because this is the first time through, nothing will be removed.
The second time through the addMarkers() method different bike locations are present in the arrayList containing the Bike data which should have bikes with different "id" values than the first time around--at least for some bikes.
This time when if(!markerList.containsKey(bike.getId())){ is called some of the values for bike.getId() will still be in the markerList --> nothing is done! But, some bike.getId() values will not be in the list--> these will be add to the list and markers added to the map. This means you will have more than 25 markers and more than 25 elements in markerList.
In the second loop for (Long key : markerList.keySet()){ a check is performed to see if the arrayList contains the keys from the markerList. If not in arrayList then that marker is removed from the markerList and the from the map.
What you must do is, before adding the new markers, you must clear the map by calling mGoogleMap.clear() . And you have to do this every time before adding the new markers.

How to add marker in android google maps v2 without refreshing the map?

The problem is when i add markers during the runtime of app ,the map changes its position
for (News news : newslist) {
String inputString=news.getTitle().trim();
int maxLength = (inputString.length() < MAX_CHAR)?inputString.length():MAX_CHAR;
inputString = inputString.substring(0, maxLength)+"...";
marker = googleMap.addMarker(new MarkerOptions().position(new LatLng(
news.getLatitude(),news.getLongitude())).title(inputString).icon(BitmapDescriptorFactory.fromResource(R.drawable.world)));
markersList.add(marker);
}

Can I add the same marker in different ArrayLists?

I would like to know how can I add for example the same marker into different array lists. I have let's say 5 array lists and 7 markers. I am adding the markers to the map, adding each marker to it's corresponding list and filter to display or hide the markers according to the list selected by the user. Everything works fine when the lists contain different markers, but when I have common markers in two lists the common marker will be kept only in the last list added.
List<Marker> firstList = new ArrayList<>();
List<Marker> secondList= new ArrayList<>();
List<Marker> thirdList= new ArrayList<>();
List<Marker> fourthList= new ArrayList<>();
List<Marker> fifthList= new ArrayList<>();
Marker marker1 = mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1"));
Marker marker2 = mMap.addMarker(new MarkerOptions().position(latLong2).title("MARKER2").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker2"));
Marker marker3 = mMap.addMarker(new MarkerOptions().position(latLong3).title("MARKER3").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker3"));
Marker marker4 = mMap.addMarker(new MarkerOptions().position(latLong4).title("MARKER4").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker4"));
Marker marker5 = mMap.addMarker(new MarkerOptions().position(latLong5).title("MARKER5").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker5"));
Marker marker6 = mMap.addMarker(new MarkerOptions().position(latLong6).title("MARKER6").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker6"));
Marker marker7 = mMap.addMarker(new MarkerOptions().position(latLong7).title("MARKER7").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker7"));
firstList.add(marker1);
secondList.add(marker2);
secondList.add(marker3);
thirdList.add(marker1);
thirdList.add(marker4);
fourthList.add(marker5);
fifthList.add(marker6);
fifthList.add(marker7);
fifthList.add(marker1);
fifthList.add(marker2);
So when I filter the markers by lists for example if I filter for firstList no marker will be displayed, if I filter for thirdList only marker4 will be displayed and if I filter for fifthList than al the markers will be displayed including marker1. So the list keeps only the last added common items. So how can I make it so that the lists will keep all their objects even though they are common to other lists?
Thanks in advance!
UPDATE:
I don't know if this is exactly what you suggested, but I did this and now the filter is working properly. By doing this I have another problem.
List<Marker> firstList = new ArrayList<>();
List<Marker> secondList= new ArrayList<>();
List<Marker> thirdList= new ArrayList<>();
List<Marker> fourthList= new ArrayList<>();
List<Marker> fifthList= new ArrayList<>();
firstList.add(mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1")));
secondList.add(mMap.addMarker(new MarkerOptions().position(latLong2).title("MARKER2").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker2")));
secondList.add(mMap.addMarker(new MarkerOptions().position(latLong3).title("MARKER3").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker3")));
thirdList.add(mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1")));
thirdList.add(mMap.addMarker(new MarkerOptions().position(latLong4).title("MARKER4").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker4")));
fourthList.add(mMap.addMarker(new MarkerOptions().position(latLong5).title("MARKER5").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker5")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong6).title("MARKER6").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker6")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong7).title("MARKER7").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker7")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong2).title("MARKER2").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker2")));
Now I want to add OnInfoWindowClickListener to my markers info window. Each marker must open a new activity. Before doing this modification I was adding all the markers to the a private Map allMarkersMap = new HashMap(); and I was adding every marker to the HashMap with allMarkersMap.put(marker1, Marker1.class);
GoogleMap.OnInfoWindowClickListener MyOnInfoWindowClickListener = new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Class cls = allMarkersMap.get(marker);
Intent intent = new Intent(MainActivity.this, cls );
startActivity(intent);
}
};
How can I do this now?
Your markers have the same references, that's what leads you to this behaviour.
Instead of creating and passing markers, you should create MarkerOptions and when you add a marker to your array list then create a new Marker. MarkerOptions is the object which keep the informations you want for your markers, that's the only object which should be instantiated once.

How can i access all marker on my GoogleMap-Object (android maps v2) and set them (in)visible?

i am currently trying to implement an ActionBar-Button that on usage sets all my markers on my GoogleMap-object visible or invisible. My problem is that i don't know how i can get a reference to all my markers once they have been created and are shown on my map. Im looking for a solution where i stash all my marker-objects into an array, that i can access in other parts of my code aswell. is this approach reasonable?
here is what i am thinking of:
private Marker[] mMarkerArray = null;
for (int i = 0; i < MainActivity.customers.size(); i++) {
LatLng location = new LatLng(mData.lat, mData.lng);
Marker marker = mMap.addMarker(new MarkerOptions().position(location)
.title(mData.title)
.snippet(mData.snippet));
mMarkerArray.add(marker);
}
and set all my markers invisible on within another method:
for (int i = 0; i < mMarkerArray.length;; i++) {
mMarkerArray[i].setVisible(false);
}
it refuses to add the markers to a Marker[]-array. how can i achieve it?
mMarkerArray.add(marker) doesnt work
i figured out an answer that also regards my customerList having customers without coordinates --> (0,0;0,0). inspired by this blog.
initialize ArrayList:
private ArrayList<Marker> mMarkerArray = new ArrayList<Marker>();
add marker to my map and to the mMarkerArray:
for (int i = 0; i < MainActivity.customers.size(); i++) {
Customer customer = MainActivity.customers.get(i);
if (customer.getLon() != 0.0) {
if (!customer.isProspect()) {
Data mData= new Data(customer.getLat(),customer.getLon(),customer.getName(),
customer.getOrt());
LatLng location = new LatLng(mData.lat, mData.lng);
Marker marker = mMap.addMarker(new MarkerOptions().position(location)
.title(mData.title)
.snippet(mData.snippet));
mMarkerArray.add(marker);
}}}
set all markers not-visible
for (Marker marker : mMarkerArray) {
marker.setVisible(false);
//marker.remove(); <-- works too!
}
Replace
private Marker[] mMarkerArray = null;
with
private List<Marker> mMarkerArray = new ArrayList<Marker>();
and you should be fine.
If you use Android Maps Extensions, you can simply iterate over all markers using
for (Marker marker : googleMap.getMarkers()) {
marker.setVisible(false);
}
without having your own List of all Markers.
You can keep a Collection of OverlayItem within your Activity or Fragment and then call MapView.getOverlays().clear() to make them "invisible" and then add them back to make them visible again. Call MapView.invalidate() after each action to cause the map to be repainted.

Placing different marker icons for different users in android application on GoogleMaps

i want to add points on a the map in android application , i get the points (Latitude,Longitude) from web service and want to place them on the map with markers but the problem that i have two kinds of users so i want to place one of them with a marker different than the other users
you could do some thing like that:
for (Task tempTask : SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository())
{
LatLng latlng = new LatLng(tempTask.getLatitude(),tempTask.getLongtitude());
if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_WAITING))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_blue)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_IN_PROGRESS))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_bordo)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_ON_THE_WAY))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_turkiz)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_COMPLETE))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_orange)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_FAILED))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
}
}
as you see I am setting a different icon with a different color for the marker depending on it's status.
you could do some thing similar, just change the check you preform.

Categories

Resources