I am developing an Android mapping application and I have managed to work Google's ItemizedOverlay tutorial into my code. I'm plotting my little circle on the map nicely.
However, I would like to label my tiny symbol with its title -- all the time. I'm going to be dropping a few different symbols on my map and the able to display their labels would be a BIG help. I got the onTap method working, but when you tap the screen, it takes over the whole display -- which is not very helpful.
I haven't managed to find a thing about this on the internet, so I'm not optimistic, but if anybody's got any sort of suggestion, that would be much appreciated.
Not sure what onTap has to do with this or what you mean by "it takes over the whole display," but I think to display labels you'll have to draw them yourself. You could do this in a couple ways. One would be to override the ItemizedOverlay.draw method directly, and iterate through each one of your GeoPoints and draw the title directly onto the Canvas at some small offset to that location. Another possible way would be to return a custom marker; instead of just returning the symbol, you could create a Picture by drawing the circle and then drawing some text next to it, and then you would be able to wrap this in a PictureDrawable and use that as the marker for your overlay item.
My onTap method looks like this:
protected boolean onTap (int index)
{
OverlayItem item = mOverlays.get (index);
AlertDialog.Builder dialog = new AlertDialog.Builder (mContext);
dialog.setTitle (item.getTitle());
dialog.setMessage (item.getSnippet());
dialog.show();
return true;
}
The calling application passes in its context (this) when it instantiates the Itemized Overlay. I'm still not really sure what that means, but I save the passed-in context in mContext, and use it in onTap(). I can't really take credit for it, naturally, since somebody else posted in to the Internet. It does display the Overlay's information on the screen, but (unfortunately for me) not in a manner that I'm happy with.
I'm working on overriding the draw method, but that's been much interrupted and is still in its early stages. I'll post it here if there's any interest.
R.
Related
im new to this android things. And i have to develop an application that can help an autism to learn numbers. I have a few ideas and I've been trying to learn and implement the code. But it's failed. The question is how can i apply the motion code or sprite to draw a numbers or letter? For example like this, i wanna make the penguin move through the line and draw a number nine.
There is example from mybringback.com which is the image move to draw a rectangle. How can i implement it to draw a number? Im sorry if i asking too much, i just trying to get some ideas.
I think that you should first build an utility program, in order to create the "path vector".
What I mean by path vector is simply a vector of Points (where a point has x value, and y value). And your utility should let you draw whatever you want, with a simple pen. You should draw on surface and store points when mouse is down, and ignore points when mouse is up.
Then, in the main program, you will just have to read at the path of your number/letter.
I've tried to implement something like this for the Sugar OLPC platform, without serializing path into files : I was able to draw, and to view the animation. And I used the process I've just described you.
Hope it can help you.
P.S : I used the word mouse, but you guessed that I talk about finger ...
There are various ways to achieve animation effects. One approach that is quite versatile involves creating a custom View or SurfaceView in which you Override the onDraw method. Various tutorials can be found on this; the official Android discussion of it is here:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#on-view
Your implementation will look something like this:
// Find elapsed time since previous draw
// Compute new position of drawable/bitmap along figure
// Draw bitmap in appropriate location
// Add line to buffer containing segments of curve drawn so far
// Render all segments in curve buffer
// Take some action to call for the rendering of the next frame (this may be done in another thread)
Obviously a simplification. For a very simplistic tutorial, see here:
http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/1733/
Note that different implementations of this technique will require different levels of involvement by you; for example, if you use a SurfaceView, you are in charge of calling the onDraw method, whereas subclassing the normal View lets you leave Android in charge of redrawing (at the expense of limiting your ability to draw on a different thread). In this respect, Google remains your friend =]
I have an android app I have written, that basically has a custom overlay, in this overlay I have overwritten the DRAW method to draw something on a canvas and at the end of my DRAW override, I call the super DRAW passing in the canvas I have painted.. the result is, every time the map is modified in anyway my custom overlay is redrawn as expected.
Now, I am trying to accomplish the same thing on iPhone and am getting a little confused.
Basically I need something drawn in the top right corner of the map every time a redraw occurs of the map. The drawin will change change with every update as well so can't simply be put a view over the map and pass touches through etc.
So, I guess the question is, what is the euivalent of the DRAW method in iOS.. how would I roughly accomplish this same thing? There are MKOverlayView in the API, but there seem to be some significant differences.. So, how do I put something say at 10x10 over the map, whos size is variable and make sure every time the map is moved, scaled or otherwised interacted with this object is redrawn at location 10x10 on the screen.
The docs for MKMapView state that you should not subclass MKMapView, so you can't / shouldn't override the drawRect method. You could add a UIView, though, and override drawRect to do your custom drawing.
I'm new to Android but I have some Java knowledge and I've started reading the Android documentation and some book but I'm in the very beginning. But even if I've read more, it would be hard to know the recommended way to do what I want without real experience in Android programming...
What I need is to have some kind of surface where I can paint/draw/whatever lots of images, probably PNGs with transparency. I need to be able to pan around and zoom this surface with all those images. I also need to be able to click on any of those images and know which one was clicked.
I've been reading the One Finger Zoom Tutorial from Sony Ericsson developers and it looks like a good solution for what I want to do. But I'm a little confused because that code is for a single bitmap image and I need to have lots of them drawn on some surface/canvas.
I'm thinking of two ways to accomplish this:
Adapt the code somehow so I can draw each image on the Canvas (from onDraw()) but that's where I'm confused, I don't see how can I accomplish this cause the current code is simple for a single image, there's not much to account for.
Overlay all my images into a single bitmap and use that bitmap as a background for the Canvas (once again, from onDraw()). I could save those images position in the Canvas into an Array or something and use that as reference to see if the user clicked in any of them. I suppose this is possible this way?
So, in reality, here's what I'm asking:
If the first described method above is better, please help me understand how can I achieve such a thing.
If the second one is better, could you give me some hints how can I overlay all those images, save their positions and detect them with a touch event?
If there's a better a way to handle all this, I would love to hear about it.
This is for a very basic game I'm thinking of developing and before you go that way, OpenGL is too much for what I need and I want to take one step at a time. First learn Android programming and someday learn OpenGL.
Why not see if you can use (or extend) the ItemizedOverlay as used with a MapView. The ItemizedOverlay enables you to add 'markers', usually PNG drawables which can all be different. They respond to Touch events or Gestures and are laid out according to screen locations (which are translated into map coordinates via the MapProjection). (I use them in a mapping app I'm working on). The layouts have their own draw() method which does require a MapView, but you may be able to null this or possibly disable any map layer from showing and show some other kind of background - some experimentation needed here.
Let me get this straight. You want multiple images each of them touchable?
If so, maybe you should think a little bit more abstractly.
Maybe it would be easier to treat each images as a view object (just an idea) since view objects already come built in with onTouchListeners
Your first solution (inserted below) is best in my opinion.
Adapt the code somehow so I can draw each image on the Canvas (from onDraw()) but that's where I'm confused, I don't see how can I accomplish this cause the current code is simple for a single image, there's not much to account for.
Depending on your use-case, I'd make your View contain your images in an appropriate structure, An ArrayList (if you're not doing to many random inserts/deletions) maybe. On the onDraw() method just iterate down this list of bitmaps and draw each of them.
In order to make them touchable, use the setOnTouchListener method (or override onTouch() if you're subclassing View, I didn't read the link you provided) to provide a handler that will be called whenever the view is called. The handler will be given the location of every touch event, which you can use to determine which, if any, image was touched. How you test collisions will of course depend upon the specifics of your game.
This method is not the cleanest, but it is the simplest (and fastest depending on how you do it!). Personally, OpenGL is intimidating but overcoming the learning curve will pay off greatly in the future (like vim or emacs!), it was made for this kind of use-case.
For more information on handling touch events, see this link. http://developer.android.com/guide/topics/ui/ui-events.html
This question is a bit old, but I have one solution and still one question :-)
In order to manage to drop several images, I create an ArrayList with this class (ColorBall):
private Bitmap img; // the image of the ball
private int coordX = 0; // the x coordinate at the canvas
private int coordY = 0; // the y coordinate at the canvas
private int id; // gives every ball his own id, for now not necessary
private static int count = 1;
private boolean goRight = true;
private boolean goDown = true;
private int height = 50;
private int width = 50;
private Rect mRectSrc = new Rect();
private Rect mRectDst = new Rect();
In the view, when I click, if I have clicked inside the limits of an image, I modify the coordX and coordY (easy to check with if (x > ball.getX() && x < ball.getX()+ball.getWidth() && y > ball.getY() && y < ball.getY()+ball.getHeight()). ).
In the OnDraw event, I just go through all the elements in the Array and I paint them where it corresponds.
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
However, I'm still stucked with the zooming. I'm trying to solve it as in the following post (http://stackoverflow.com/questions/6422759/zoom-a-bitmap-with-drawbitmap-and-rectangles-android), but I'm still blocked with it :-(
Cheers
Does anybody know the relationship between the members of an Android Itemized Overlay list and when draw() is called. Specifically, I'm trying to find out if draw is called once for each Overlay or once only for the whole set.
Thanks,
R.
I overrode draw() in my ProjectOverlay extends ItemizedOverlay class and in it I loop though the population of the overlay list like so:
for (int ctr = 0; ctr < overlayList.size(); ctr++)
I haven't tested other ways of drawing the screen, because this seems to be working fine.
As far as I understand it, draw() is called once each time the map moves (when you touch the screen and move your finger). By once, I mean that if you want to draw text on each overlay (a name or something) you must loop through your ArrayList in your overriden draw().
As far as I can tell, it's called twice for each visible item. However, I haven't been able to find out which item it's being called for, thus making any recalculation of drawable items difficult (to say the least)
Trying to make an google maps overlay in an android program. Inside of my overlay's draw method, I have two ways of adding a pin. One of them works, and one does not. Unfortunately, the one that does not work is also the only one that has the ability to add a shadow! Any help?
#Override
public void draw(android.graphics.Canvas canvas, MapView mapView,
boolean shadow) {
Point po = mapView.getProjection().toPixels(mapView.getMapCenter(),
null);
// This does _not_ work, but I would really like it to!
drawAt(canvas, mapView.getResources().getDrawable(R.drawable.map_marker_v),
po.x, po.y, false);
// This does work, but only does half the job
canvas.drawBitmap(BitmapFactory.decodeResource(mapView.getResources(),
R.drawable.map_marker_v), po.x, po.y, null);
}
Edit: fixed type
I think your problem may simply be that you haven't set the bounds on the drawable in drawAt(). You can either manually set the bounds using Drawable.setBounds(), or you can use ItemizedOverlay's convenience methods boundCenter() or boundCenterBottom().
I believe the reason the second method works is because with a decoded Bitmap you don't have to specify the bounds of the Drawable.
At first sight, nothing stands out to me as to what could be causing your pin to not draw. But, I might have found a temporarily solution.
Looking on google lead me to this post where an user posts their version of an Overlay with the ability to add an icon along with a shadow. It might be what your looking for.
Hope this helps.