Android Google Map API V2 : Open Custom Info Window on Right Side of Marker
Asked Answered
N

2

28

I have Integrated Google MAP API V2 for Android. I want to have a Custom Info Window at Onclick of My Marker. Up to that it is fine. i have integrate it.

What I want : I want to Show my Custom Info Window on Right Side of Marker Instead of Top of the Marker.

Below is the Code I am Using :

public class MainActivity extends FragmentActivity {

    private MainMapFragement mapFragment;
    private HashMap<Marker, EventInfo> eventMarkerMap;


    Marker ThirdMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapFragment = new MainMapFragement();
        // FragmentTransaction ft = getFragmentManager().beginTransaction();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.map, mapFragment);
        ft.commit();

    }

    @Override
    protected void onStart() {
        super.onStart();
        setUpEventSpots();
    }

    private void setUpEventSpots() {
        // I'm going to make 2 EventInfo objects and place them on the map
        EventInfo firstEventInfo = new EventInfo(new LatLng(50.154, 4.35),
                "Right now - event", new Date(), "Party");
        EventInfo secondEventInfo = new EventInfo(new LatLng(51.25, 4.15),
                "Future Event", new Date(1032, 5, 25), "Convention");

        EventInfo thirdEventInfo = new EventInfo(new LatLng(23.25, 72.15),
                "Our Next Event", new Date(), "Ahmedabad-India");
        // this date constructor is deprecated but it's just to make a simple
        // example

        Marker firstMarker = mapFragment.placeMarker(firstEventInfo);
        Marker secondMarker = mapFragment.placeMarker(secondEventInfo);
        ThirdMarker = mapFragment.placeMarker(thirdEventInfo);
        eventMarkerMap = new HashMap<Marker, EventInfo>();
        eventMarkerMap.put(firstMarker, firstEventInfo);
        eventMarkerMap.put(secondMarker, secondEventInfo);
        eventMarkerMap.put(ThirdMarker, thirdEventInfo);

        // add the onClickInfoWindowListener
        mapFragment.getMap().setOnInfoWindowClickListener(
                new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        EventInfo eventInfo = eventMarkerMap.get(marker);
                        Toast.makeText(
                                getBaseContext(),
                                "The date of "
                                        + eventInfo.getName()
                                        + " is "
                                        + eventInfo.getSomeDate()
                                                .toLocaleString(),
                                Toast.LENGTH_LONG).show();

                    }
                });



        // Custom Bhavesh
        mapFragment.getMap().setInfoWindowAdapter(new InfoWindowAdapter() {

            private final View window = getLayoutInflater().inflate(
                    R.layout.ballonoverlly, null);

            @Override
            public View getInfoWindow(Marker marker) {
                EventInfo eventInfo = eventMarkerMap.get(marker);

                String title = marker.getTitle();
                TextView txtTitle = ((TextView) window
                        .findViewById(R.id.textview_name));
                if (title != null) {
                    // Spannable string allows us to edit the formatting of the
                    // text.
                    SpannableString titleText = new SpannableString(title);
                    titleText.setSpan(new ForegroundColorSpan(Color.RED), 0,
                            titleText.length(), 0);
                    txtTitle.setText(titleText);
                } else {
                    txtTitle.setText("");
                }

                // TextView txtType = ((TextView) window
                // .findViewById(R.id.textview_aboutme));
                // if (eventInfo.getType() != null)
                // txtType.setText(eventInfo.getType());

                return window;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // this method is not called if getInfoWindow(Marker) does not
                // return null
                return null;
            }
        });
    }

}

xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RlMain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >


    <LinearLayout
        android:id="@+id/imageview_comment"
        android:layout_width="150dp"
        android:layout_height="62dp"
        android:background="@drawable/map_comment_back"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:scaleType="fitXY"
        android:visibility="visible" >

        <TextView
            android:id="@+id/textview_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:text="Bhavesh Patadiya"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textview_aboutme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:maxLines="2"
            android:text="How&apos;s Going?"
            android:textColor="#FFFFFF"
            android:textStyle="normal" />
    </LinearLayout>
</LinearLayout>

Below is the Screen Shot of What I am getting : enter image description here

Now , I want to have my custom InfoWindow to the Right Side of the Marker instead of Top.

All Helps are Appreciated.

Thanks in Advance.

Nicias answered 3/5, 2013 at 11:29 Comment(1)
Try with this: Creating an image(background) with half left side blank and half right side according to u --> then add content(TextView) at your right side portion. I hope it will take the right of the marker because left side looks blankPanacea
B
7

Edit 2:

Starting from September update (v12 or 3.2.65) of the Google Maps Android API v2 MarkerOptions.infoWindowAnchor was added. There are quite a few new functions, so check release notes too.

Edit 3:

Unfortunatelly this addition doesn't solve your problem yet. Added new issue for that.


This is not currently possible.

Trying to work around it by putting a View over MapFragment can be a good solution if users are not allowed to move the map. If they can, you will see some bad lags on repositioning when using either "push techonology" onCameraChange or polling with Handler and map.getCameraPosition().

Same problems if you try to use additional marker with transparent icon to show info window on when the visible is clicked, but it would be more fun to see it actually implemented.

If you can live without info window on the right for now, I would suggest starring this feature already requested and waiting.

Edit:

As of 3.1.36 (rev. 7) of the API v2 you can change the icon of marker. This means you can make two icons: one normal, and another that looks like you have an info window open on the right. Disabling opening default info window and using this instead might work for you.

Balneology answered 3/5, 2013 at 14:37 Comment(0)
S
0

You can do by simple convert marker.getPosition() to screenPosition like below code

Projection projection = map.getProjection();
LatLng markerLocation = marker.getPosition();
Point screenPosition = projection.toScreenLocation(markerLocation);

int x = screenPosition.x +marker. markerImage.getWidth();

You can ref below link for more infomation (which convert getPosition to projection):

How to get screen coordinates from marker in google maps v2 android

Simony answered 3/5, 2013 at 12:17 Comment(2)
@BhaveshPatadiya, Are you done with this? I want to try place the Info Window to top of screen.Minuet
how can i put a view on map ? As when i place a fragment on map using the code inflater.inflate(R.id.overlay_layout,(viewGroup)myViewGroup) it does show the view on marker, but my it has an issue that when the layout is along the sides of the screen view gets wraped i.e. if textview is having text textview1 then some text are in one line and some text are in 2nd line.Can some one help me to resolve thisLacewing

© 2022 - 2024 — McMap. All rights reserved.