Can´t show multiple markers !!

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Can´t show multiple markers !!

Postby danthorpe » Tue Sep 22, 2009 4:25 pm

Hi all ..

I´ve been trying for days now to show multiple markers on my map ... however, it shows only the first one.. Please, any help would be appreciated ..

My code is :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mapOverlays = mapView.getOverlays();
  3.  
  4.         drawable = this.getResources().getDrawable(R.drawable.androidmarker);
  5.  
  6.         itemizedOverlay = new MapItemizedOverlay(drawable);
  7.  
  8.        
  9.  
  10.         OverlayItem overlayitem = new OverlayItem(p, "", "");
  11.  
  12.        
  13.  
  14.         itemizedOverlay.addOverlay(overlayitem);        
  15.  
  16.         mapOverlays.add(itemizedOverlay);
  17.  
  18.  
  19.  
  20. ...
  21.  
  22.  
  23.  
  24. for (AddressRow addressRow : l) {
  25.  
  26.                 lat = addressRow.latitude * 1E6;
  27.  
  28.                 lng = addressRow.longitude* 1E6;               
  29.  
  30.            
  31.  
  32.             p = new GeoPoint(lat.intValue(), lng.intValue());            
  33.  
  34.             try
  35.  
  36.             {
  37.  
  38.                 Location.distanceBetween(-23.557375,-46.660525, lat, lng, distancia);
  39.  
  40.             }
  41.  
  42.             catch (IllegalArgumentException i){mb = new MessageBox(this, "Atencao !", "Erro ao calcular distancia");};
  43.  
  44.            
  45.  
  46.                 OverlayItem overlayitem = new OverlayItem(p, "", "");
  47.  
  48.                 overlayitem.setMarker(drawable);
  49.  
  50.                
  51.  
  52.                 itemizedOverlay.addOverlay(overlayitem);
  53.  
  54.                 //mapView.getOverlays().add(itemizedOverlay);
  55.  
  56.                 mapOverlays.add(itemizedOverlay);
  57.  
  58.            
  59.  
  60.                 }
  61.  
  62.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And the itimizedOverlay class is this one :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.android;
  3.  
  4.  
  5.  
  6. import java.util.ArrayList;
  7.  
  8.  
  9.  
  10. import android.graphics.drawable.Drawable;
  11.  
  12.  
  13.  
  14. import com.google.android.maps.ItemizedOverlay;
  15.  
  16. import com.google.android.maps.OverlayItem;
  17.  
  18.  
  19.  
  20. public class MapItemizedOverlay extends ItemizedOverlay {
  21.  
  22.        
  23.  
  24.         private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
  25.  
  26.  
  27.  
  28.         public MapItemizedOverlay(Drawable defaultMarker) {
  29.  
  30.                 super(boundCenterBottom(defaultMarker));
  31.  
  32.                 // TODO Auto-generated constructor stub
  33.  
  34.         }
  35.  
  36.        
  37.  
  38.         public void addOverlay(OverlayItem overlay) {
  39.  
  40.             mOverlays.add(overlay);
  41.  
  42.             populate();
  43.  
  44.         }
  45.  
  46.  
  47.  
  48.         @Override
  49.  
  50.         protected OverlayItem createItem(int i) {
  51.  
  52.                 return mOverlays.get(i);
  53.  
  54.         }
  55.  
  56.  
  57.  
  58.         @Override
  59.  
  60.         public int size() {
  61.  
  62.                 return mOverlays.size();
  63.  
  64.         }
  65.  
  66.  
  67.  
  68. }
  69.  
  70.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Something wrong with the code above ?? If something is missing, please let me know...

Tks in advance..
danthorpe
Freshman
Freshman
 
Posts: 2
Joined: Tue Sep 22, 2009 4:18 pm

Top

Postby andreleitao » Mon Sep 28, 2009 5:45 pm

try to put your MapItemizedOverlay object in another place (as attribute is a good idea), maybe as following:

...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private MapItemizedOverlay itemizedOverlay = new MapItemizedOverlay(drawable);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

...

Now you can add how many overlayItems you want to show on the screen:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. OverlayItem pin1 = new OverlayItem(p, **latitude1**, **longitude1**);
  2.  
  3.         OverlayItem pin2 = new OverlayItem(p, **latitude2**, **longitude2**);
  4.  
  5.  
  6.  
  7.        //we want to add two pins over the map
  8.  
  9.         itemizedOverlay.addOverlay(overlayitem);
  10.  
  11.         itemizedOverlay.addOverlay(overlayitem);
  12.  
  13.        
  14.  
  15.  
  16.  
  17. //when you add all overlayitems you want, you add the MapItemizedOverlay object to mapOverlays:
  18.  
  19.         mapOverlays.add(itemizedOverlay);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Hope it helps.

Good luck :)
andreleitao
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Mar 10, 2009 9:56 pm
Location: Recife - Brazil

Postby pria » Thu Mar 25, 2010 9:54 am

Did u get a solution for this? I am also not able to add multiple markers :(
Plz help!
pria
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Mar 02, 2010 1:37 pm
Location: Pune

Postby vik » Tue Mar 30, 2010 2:01 pm

guys try
this if it works
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     MapOverlay[] itemized = new MapOverlay[len];
  3.  
  4.  
  5.  
  6.           for(int i = 0; i < len; i++)
  7.  
  8.           {
  9.  
  10.                       itemized[i] = new MapOverlay(drawable);
  11.  
  12.                           double latitude = 0;
  13.  
  14.                           try
  15.  
  16.                           {
  17.  
  18.                                   latitude = Double.parseDouble(lat[i]);
  19.  
  20.                           }
  21.  
  22.                           catch(Exception e)
  23.  
  24.                           {
  25.  
  26.                           }
  27.  
  28.                           double longitude = 0;
  29.  
  30.                           try
  31.  
  32.                           {
  33.  
  34.                                   longitude = Double.parseDouble(lon[i]);
  35.  
  36.                           }
  37.  
  38.                           catch(Exception e)
  39.  
  40.                           {
  41.  
  42.                           }
  43.  
  44.                          
  45.  
  46.                           GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6));
  47.  
  48.                        OverlayItem overlayitem = new OverlayItem(point, "", "");
  49.  
  50.                        itemized[i].addOverlay(overlayitem, i);
  51.  
  52.                        mapOverlays.add(itemized[i]);
  53.  
  54.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
User avatar
vik
Senior Developer
Senior Developer
 
Posts: 141
Joined: Wed Sep 09, 2009 7:32 am

Top

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest