max4ever
max4ever

Reputation: 12142

android custom map marker bounds

Hello i am using a resized version of this marker( http://www.clker.com/clipart-orange-pin-4.html ) for showing markers in google maps on android.

The problem is i don't know how to make the marker point match the coordinates. The arrow point is at about 1/5 of the Width coordinates and MAX of the Height.

here is my class

public class GestionaleItemizedOverlay extends com.google.android.maps.ItemizedOverlay {

    public GestionaleItemizedOverlay(Drawable defaultMarker, Context context) {
        //super(boundCenterBottom(defaultMarker));
        super(boundCenter(defaultMarker));
        this.mContext = context;
    }
...

And this

    this.marker_poi = this.getContext().getResources().getDrawable(R.drawable.marker);
    this.marker_poi.setBounds(this.marker_poi.getIntrinsicWidth() / 2, this.marker_poi.getIntrinsicHeight(), this.marker_poi.getIntrinsicWidth() / 2, 0);
 new GestionaleItemizedOverlay(this.poi, this.context);

Do i need to setBounds on the marker before passing it to the constructor? and why does super(defaultMarker) makes all the markers not to show ?

Upvotes: 1

Views: 1015

Answers (2)

Speckpgh
Speckpgh

Reputation: 3372

The previous answer is right, you need to play with the BOUNDS value of your marker.

As I am sure you know the bounding rectangle determines the size of the object... The marker on your map is going to lay itself out with upper left coordinate of your marker at the GPS coordinate if you just would call a normal setbounds on the size of your image.

What you need to do is modify the bounds on your image so that the part of the image you wnat is over the point on the map you want.

So if your resized image is say 20 x 20, and your actual arrow tip (assuming the image is an arrow, and you want it lined up exactly with the point on the map) is at say X=4 and Y=10, you need to modify the bounds of your image accordingly to "move" the image where you want it.

Since 0,0 would put the image 4 pixels to the right, and 10 pixels below the actual point on the map you associate it with, your bounds should not be 0,0,X,Y but should be -4,-10,X size of image-4,Y size of image-10 (in this exampe case the xsize of image and ysize of image are both 20, but should use the values appropriate for your case.)

Upvotes: 2

Warpzit
Warpzit

Reputation: 28162

Yes you need to setBounds but you can do it in the constructor, this is what I do in the constructor to set it to the left bottom corner:

public class CustomOverlay extends ItemizedOverlay<CustomOverlayItem> {
    public CustomOverlay(Drawable defaultMarker, Context context) {
        super(defaultMarker);
        defaultMarker.setBounds(0, -defaultMarker.getIntrinsicHeight(), defaultMarker.getIntrinsicWidth(), 0);
    }

You'll need to add a little extra to the left side of the bounds to make center point at the right position. You should remember this is set in px so make some kind of DP converter so it is set properly for your different images for each screen size.

About the second question, you'll need a context in order to know where to add the overlay. Without the context you just create your overlay without giving it any reference to where it should be used (you could add this later if you wanted to).

Upvotes: 1

Related Questions