Add multiple items to ItemizedOverlay

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

Add multiple items to ItemizedOverlay

Postby KaiserQ » Wed Aug 04, 2010 7:30 pm

Hi everybody.

I followed the google tutorial (http://developer.android.com/resources/tutorials/views/hello-mapview.html) and I want to add multiple items on my HelloItemizedOverlay but my code don't work. I'm using arrays to store points and items then add items to the ItemizedOverlay.

The HelloItemizedOverlay class from google's tutorial :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.hellogooglemaps;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.AlertDialog;
  6. import android.content.Context;
  7. import android.graphics.drawable.Drawable;
  8.  
  9. import com.google.android.maps.ItemizedOverlay;
  10. import com.google.android.maps.OverlayItem;
  11.  
  12. public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
  13.  
  14.     private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
  15.     private Context mContext;
  16.    
  17.     public HelloItemizedOverlay(Drawable defaultMarker) {
  18.         super(boundCenterBottom(defaultMarker));
  19.     }
  20.    
  21.     public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
  22.         super(boundCenterBottom(defaultMarker));
  23.         mContext = context;
  24.     }
  25.  
  26.     @Override
  27.     protected OverlayItem createItem(int i) {
  28.       return mOverlays.get(i);
  29.     }
  30.  
  31.     @Override
  32.     public int size() {
  33.       return mOverlays.size();
  34.     }
  35.    
  36.     @Override
  37.     protected boolean onTap(int index) {
  38.       OverlayItem item = mOverlays.get(index);
  39.       AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  40.       dialog.setTitle(item.getTitle());
  41.       dialog.setMessage(item.getSnippet());
  42.       dialog.show();
  43.       return true;
  44.     }
  45.    
  46.     public void addOverlay(OverlayItem overlay) {
  47.         mOverlays.add(overlay);
  48.         populate();
  49.     }
  50.  
  51. }
  52.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


My code to add multiple items :
mapPlaces contains Place object. The Place object have title, adress, lat and long attributes.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. GeoPoint[] mapPoints = new GeoPoint[mapPlaces.length];
  2.         OverlayItem[] mapItems = new OverlayItem[mapPlaces.length];
  3.        
  4.         for(int i=0; i<mapPlaces.length;i++){
  5.                
  6.                 double latitude = Double.parseDouble(mapPlaces[i].getLatitude());
  7.                 double longitude = Double.parseDouble(mapPlaces[i].getLongitude());
  8.                
  9.                 mapPoints[i] = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
  10.                 mapItems[i] = new OverlayItem(mapPoints[i], mapPlaces[i].getTitle(), mapPlaces[i].getAddress());                                       
  11.                 itemizedoverlay.addOverlay(mapItems[i]);
  12.  
  13.         }
  14. mapOverlays.add(itemizedoverlay);
  15.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Do you have any idea to fix this ?

Thanks,

Quentin
KaiserQ
Freshman
Freshman
 
Posts: 4
Joined: Wed Aug 04, 2010 7:19 pm

Top

Re: Add multiple items to ItemizedOverlay

Postby jonbonazza » Wed Aug 04, 2010 7:40 pm

What problem are you encountering? You never really say.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Add multiple items to ItemizedOverlay

Postby KaiserQ » Wed Aug 04, 2010 7:45 pm

There is just one item showed on the map. I want to show in map all items stored in my array
KaiserQ
Freshman
Freshman
 
Posts: 4
Joined: Wed Aug 04, 2010 7:19 pm

Re: Add multiple items to ItemizedOverlay

Postby blundell » Thu Aug 05, 2010 1:43 pm

Have you printed out the Latitude and Longitude of each item object point, as the code looks sound at a glance.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Add multiple items to ItemizedOverlay

Postby jonbonazza » Thu Aug 05, 2010 2:29 pm

The only thing that strikes me is this:
[code syntax="java"]
for(int i=0; i<mapPlaces.length;i++){

double latitude = Double.parseDouble(mapPlaces[i].getLatitude());
double longitude = Double.parseDouble(mapPlaces[i].getLongitude());

mapPoints[i] = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mapItems[i] = new OverlayItem(mapPoints[i], mapPlaces[i].getTitle(), mapPlaces[i].getAddress());
itemizedoverlay.addOverlay(mapItems[i]);
[/code]

You use a variable called mapPlaces (which above the code you say is a Place object), but can we see the code where you fill the Places object? The reason I ask is because the loop is triggered when i < mapPlaces.length(). If the places object is not filled properly, you will only trigger the loop once (or possibly zero times).

Looking at the code you provided, I can't see any errors that stick out to me.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Add multiple items to ItemizedOverlay

Postby KaiserQ » Thu Aug 05, 2010 5:13 pm

Ooops,
It's not a map problem. I get data from a json file and it's my function which get data from the file that wasn't good.
So sorry. :roll:

Quentin
KaiserQ
Freshman
Freshman
 
Posts: 4
Joined: Wed Aug 04, 2010 7:19 pm

Top

Re: Add multiple items to ItemizedOverlay

Postby blundell » Thu Aug 05, 2010 7:28 pm

Next time just put a Log.i("YourApp", "Lat:"+latitude+" Long:"+longitude); into your for loop and you would of caught the problem :-)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Add multiple items to ItemizedOverlay

Postby pranav09 » Mon Aug 09, 2010 7:20 am

Try to put mapOverlays.add(itemizedoverlay); inside the for loop in stead of outside of the for loop. Because i had faced the same problem, and i got the solution as this way. Hope, It's help you.

Thanks.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 4 guests