Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old October 28th, 2011, 04:01 PM   #1 (permalink)
New Member
 
Join Date: Oct 2011
Posts: 2
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Drawing lots of markers on a map

hi all,
I'm trying to draw on a map using markers. The problem I'm having is when I try to add lots of them, then I can't get it to run.
To illustrate my problem, let's say I want to draw a square made of 900 smaller squares (30x30).
I'm trying to add the markers to an itemizedoverlay, then drawing it.

the code is below. Any suggestion how I could do that?
Thank you very much!!

Code:
public class MyItemizedOverlay extends ItemizedOverlay{
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;

	public MyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}
	
	public MyItemizedOverlay(Drawable defaultMarker, Context context) {
		  super(defaultMarker);
		  mContext = context;
	}
	
	public void draw(android.graphics.Canvas canvas, MapView mapView,boolean shadow) {
			super.draw(canvas, mapView, false);
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

}
Code:
public class MainActivity extends MapActivity {
	MapView mapView;
	MapController mc;
	GeoPoint p;
	static Context context;
	
	int k=1;	
	List<Overlay> mapOverlays;
	MyItemizedOverlay itemizedoverlay;	
	int[] xx,yy;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        context = getApplicationContext();        
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);        
        
        mc = mapView.getController();
        String coordinates[] = {"36.186382", "-112.221909"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
        p = new GeoPoint(
				        (int) (lat * 1E6),
				        (int) (lng * 1E6));
        mc.animateTo(p);
        mc.setZoom(10);
        
        mapOverlays = mapView.getOverlays();
        Drawable squareM = this.getResources().getDrawable(R.drawable.square);
        itemizedoverlay = new MyItemizedOverlay(squareM);
        
        int n=0;
        xx = new int[30];
        yy = new int[30];
        while(n<30){
        	xx[n]=n;
        	yy[n]=n;
        	n++;
        }
        
    }
    
    protected void drawNewMarker(GeoPoint p){
    	OverlayItem overlayitem = new OverlayItem(p, "", "");
    	itemizedoverlay.addOverlay(overlayitem);
    	mapOverlays.add(itemizedoverlay);
    	mapView.postInvalidate(); 
    }
    
    protected void drawNewMarkerD(){
    	Point pointP = new Point(0,0);
        mapView.getProjection().toPixels(p, pointP);
        int xoff = 20*k;
        int yoff = 20*k;
        GeoPoint pp = mapView.getProjection().fromPixels(pointP.x + xoff, pointP.y + yoff);
        
    	OverlayItem overlayitem = new OverlayItem(pp, "", "");
    	Drawable iconM = this.getResources().getDrawable(R.drawable.icon);
    	iconM.setBounds(0,0, 50, 50);
    	overlayitem.setMarker(iconM);
    	itemizedoverlay.addOverlay(overlayitem);
    	mapOverlays.add(itemizedoverlay);
    	k++;
    	//mapView.postInvalidate(); 
    }

	@Override
	protected boolean isRouteDisplayed() {
		
		return false;
	}
	
	
    protected void drawGrid(int xoff, int yoff){
    	GeoPoint p = newGeoPointFromOffset(xoff, yoff);
    	OverlayItem overlayitem = new OverlayItem(p, "", "");
    	Drawable marker = this.getResources().getDrawable(R.drawable.grid);
    	marker.setBounds(0,0, 15, 15);
    	overlayitem.setMarker(marker);
    	itemizedoverlay.addOverlay(overlayitem);
    	mapOverlays.add(itemizedoverlay);
    }
    
    protected GeoPoint newGeoPointFromOffset(int xoff, int yoff){	
        Point screenPointP = new Point(0,0);      
        mapView.getProjection().toPixels(p, screenPointP);  
        int x = 15*xoff;
        int y = 15*yoff;    
        return mapView.getProjection().fromPixels(screenPointP.x + x, screenPointP.y + y);          	
    }
	
	public boolean onKeyDown(int keyCode, KeyEvent event){
		MapController mc = mapView.getController();
		switch(keyCode){
		case KeyEvent.KEYCODE_3:
			mc.zoomIn();
			break;
		case KeyEvent.KEYCODE_1:
			mc.zoomOut();
			break;
		case KeyEvent.KEYCODE_0:
			drawNewMarker(p);
			break;
		case KeyEvent.KEYCODE_9:	
			for(int r=0;r<30;r++){
				for(int s=0;s<30;s++){
					 drawGrid(r,s);
				}
			}  
	        mapView.postInvalidate(); 	        
	        break;
		}		
		return super.onKeyDown(keyCode, event);
	}		
}

blic is offline  
Reply With Quote
Sponsors
Old October 28th, 2011, 04:08 PM   #2 (permalink)
Senior Member
 
TheCompBoy's Avatar
 
Join Date: Oct 2010
Location: Sweden
Posts: 594
 
Device(s): Samsung Galaxy S w DarkyROM v10.1
Thanks: 4
Thanked 56 Times in 51 Posts
Send a message via Skype™ to TheCompBoy
Default

Why don't you just use some drawable picture that you painted to look as a square?
Or you could use the paint method for this.
__________________
Please check out my blogg: www.thecompboy.blogspot.com
If you want to support me please subscribe / follow the blogg!

OCalc - First App: https://market.android.com/details?id=mendel.calculator.simple&feature=search _result
TheCompBoy is offline  
Reply With Quote
Old October 28th, 2011, 04:18 PM   #3 (permalink)
New Member
 
Join Date: Oct 2011
Posts: 2
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I should have been more clear.. I do need the 900 tiny squares.
blic is offline  
Reply With Quote
Old October 29th, 2011, 10:49 AM   #4 (permalink)
Senior Member
 
TheCompBoy's Avatar
 
Join Date: Oct 2010
Location: Sweden
Posts: 594
 
Device(s): Samsung Galaxy S w DarkyROM v10.1
Thanks: 4
Thanked 56 Times in 51 Posts
Send a message via Skype™ to TheCompBoy
Default

Okay im not 100% sure but i think its possible to use the paint() method to solve this that you are intrested in creating..

Here is some links to sites where you can read about the paint():
Painting in AWT and Swing
http://www.tech-recipes.com/rx/1177/java-graphics-paint-and-repaint/

After you read that you should have some understanding what the paint is about and how it works in normal java but here is some links to how you can use it in android:
Canvas | Android Developers

This is two tutorials for the canvas:
How to use canvas in your android apps - Part 1 | Hello Android
http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-2
TheCompBoy is offline  
Reply With Quote
Old October 29th, 2011, 06:31 PM   #5 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Drawing in Android is significantly different from painting in standard Java AWT or Swing. Canvas and Drawables | Android Developers So much so, that I wouldn't bother reading up on AWT/Swing painting, lest it confuses you.
jiminaus is offline  
Reply With Quote
Old October 30th, 2011, 03:53 AM   #6 (permalink)
Senior Member
 
TheCompBoy's Avatar
 
Join Date: Oct 2010
Location: Sweden
Posts: 594
 
Device(s): Samsung Galaxy S w DarkyROM v10.1
Thanks: 4
Thanked 56 Times in 51 Posts
Send a message via Skype™ to TheCompBoy
Default

Quote:
Originally Posted by jiminaus View Post
Drawing in Android is significantly different from painting in standard Java AWT or Swing. Canvas and Drawables | Android Developers So much so, that I wouldn't bother reading up on AWT/Swing painting, lest it confuses you.
If you read the post above you then you should see i provided him with links to the Canvas and i also provided him with tutorials for it.. And pain in java AWT and Swing is good cus it gets you some ground understanding what its all about.

If you just want to raise your post count then go to The Lounge
TheCompBoy is offline  
Reply With Quote
Old October 30th, 2011, 06:54 PM   #7 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Quote:
Originally Posted by TheCompBoy View Post
If you just want to raise your post count then go to The Lounge
That's not my intent. My post count will increase in time. Not that it's worth anything to me, but thanks for allowing me another post.
jiminaus is offline  
Reply With Quote
Old October 31st, 2011, 10:01 AM   #8 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

Quote:
Originally Posted by TheCompBoy View Post
If you just want to raise your post count then go to The Lounge
That was entirely uncalled for. Please try to keep things civil. We don't need arguments spurting up.
jonbonazza is online now  
Reply With Quote
Reply

Bookmarks

Tags
map, marker


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:10 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo