5

Since I am using Google map v2 and I want to open info window on multiple makers. I have done this with single marker, and also used show info window(); but it works in on last marker and on only one. I want only and only in android.

public class MainActivity extends FragmentActivity {

    private GoogleMap googleMap;
    private final LatLng HAMBURG = new LatLng(53.558, 9.927);
    private final LatLng KIEL = new LatLng(53.551, 9.993);
    private Marker marker;
    private Hashtable<String, String> markers;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        initImageLoader();
        markers = new Hashtable<String, String>();
        imageLoader = ImageLoader.getInstance();

        options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_launcher)      //  Display Stub Image
            .showImageForEmptyUri(R.drawable.ic_launcher)   //  If Empty image found
            .cacheInMemory()
            .cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();

        if ( googleMap != null ) {

            googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

            final Marker hamburg = googleMap.addMarker(new MarkerOptions().position(HAMBURG)
                        .title("Hamburg"));
            hamburg.showInfoWindow();
            markers.put(hamburg.getId(), "http://img.india-forums.com/images/100x100/37525-a-still-image-of-akshay-kumar.jpg");

            final Marker kiel = googleMap.addMarker(new MarkerOptions()
                        .position(KIEL)
                        .title("Kiel")
                        .snippet("Kiel is cool")
                        .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.ic_launcher)));
            markers.put(kiel.getId(), "http://www.yodot.com/images/jpeg-images-sm.png");
            //kiel.showInfoWindow();

            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        }
    }

    private class CustomInfoWindowAdapter implements InfoWindowAdapter {

        private View view;

        public CustomInfoWindowAdapter() {
            view = getLayoutInflater().inflate(R.layout.custom_info_window,
                    null);
        }

        @Override
        public View getInfoContents(Marker marker) {

            if (MainActivity.this.marker != null
                    && MainActivity.this.marker.isInfoWindowShown()) {
                MainActivity.this.marker.hideInfoWindow();
                MainActivity.this.marker.showInfoWindow();
            }
            return null;
        }

        @Override
        public View getInfoWindow(final Marker marker) {
            MainActivity.this.marker = marker;

            String url = null;

            if (marker.getId() != null && markers != null && markers.size() > 0) {
                if ( markers.get(marker.getId()) != null &&
                        markers.get(marker.getId()) != null) {
                    url = markers.get(marker.getId());
                }
            }
            final ImageView image = ((ImageView) view.findViewById(R.id.badge));

            if (url != null && !url.equalsIgnoreCase("null")
                    && !url.equalsIgnoreCase("")) {
                imageLoader.displayImage(url, image, options,
                        new SimpleImageLoadingListener() {
                            @Override
                            public void onLoadingComplete(String imageUri,
                                    View view, Bitmap loadedImage) {
                                super.onLoadingComplete(imageUri, view,
                                        loadedImage);
                                getInfoContents(marker);
                            }
                        });
            } else {
                image.setImageResource(R.drawable.ic_launcher);
            }

            final String title = marker.getTitle();
            final TextView titleUi = ((TextView) view.findViewById(R.id.title));
            if (title != null) {
                titleUi.setText(title);
            } else {
                titleUi.setText("");
            }

            final String snippet = marker.getSnippet();
            final TextView snippetUi = ((TextView) view
                    .findViewById(R.id.snippet));
            if (snippet != null) {
                snippetUi.setText(snippet);
            } else {
                snippetUi.setText("");
            }

            return view;
        }
    }

    private void initImageLoader() {
        int memoryCacheSize;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            int memClass = ((ActivityManager) 
                    getSystemService(Context.ACTIVITY_SERVICE))
                    .getMemoryClass();
            memoryCacheSize = (memClass / 8) * 1024 * 1024;
        } else {
            memoryCacheSize = 2 * 1024 * 1024;
        }

        final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this).threadPoolSize(5)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .memoryCacheSize(memoryCacheSize)
                .memoryCache(new FIFOLimitedMemoryCache(memoryCacheSize-1000000))
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() 
                .build();

        ImageLoader.getInstance().init(config);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ghost Hacker
  • 139
  • 1
  • 1
  • 8

3 Answers3

10

The default markers can only show one info window at a time. However, you can easily show more, using the Maps Android library: https://developers.google.com/maps/documentation/android-api/utility/

Here is an example:

IconGenerator iconFactory = new IconGenerator(this);
mMarkerA = mMap.addMarker(new MarkerOptions().position(new LatLng(12, 34)));
mMarkerA.setIcon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Marker A")));
mMarkerB = mMap.addMarker(new MarkerOptions().position(new LatLng(13, 35)));
mMarkerB.setIcon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Marker B")));

And add

compile 'com.google.maps.android:android-maps-utils:0.4+'

to your gradle file.

lenooh
  • 10,364
  • 5
  • 58
  • 49
0

According to Google Maps Android v2

An info window allows you to display information to the user when they tap on a marker on a map. By default, an info window is displayed when a user taps on a marker if the marker has a title set. Only one info window is displayed at a time. If a user clicks on another marker, the current window will be hidden and the new info window will be displayed. You can show an info window programmatically by calling showInfoWindow() on the target marker. An info window can be hidden by calling hideInfoWindow()

do like:

 LatLng latlang = new LatLng(lati, longi);
    if (map != null) {
    Marker marker = map.addMarker(new MarkerOptions()
    .position(latlang)
    .title(getResources().getString(R.string.titleProduct))
    .snippet(getResources().getString(R.string.contentProduct))
    .icon(BitmapDescriptorFactory
    .fromResource(R.drawable.map_pointer)));
     marker.showInfoWindow();

}

and repeat this same with as much as you have map pointers.

Fakhar
  • 3,946
  • 39
  • 35
-1

First comment this line.

googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

then add markers on map using this code

Marker marker = googleMap.addMarker(new MarkerOptions()
                 .position(HAMBURG)
                 .title("Hamburg")
                 .snippet("welcome to Map")
                 .icon(BitmapDescriptorFactory.fromResource(R.drawable.start_pin)).draggable(false));
  googleMap.addMarker(new MarkerOptions()
                 .position(KIEL)
                 .title("KIEL")
                 .snippet("welcome to Map")
                 .icon(BitmapDescriptorFactory.fromResource(R.drawable.start_pin)).draggable(false));

marker.showInfoWindow();

then comment the other code and execute it once. Then i will give CustomAdapter code

NARESH REDDY
  • 682
  • 4
  • 11
  • I think in Android only one info window will shown at a time.When first marker is clicked it will show title as "Hamburg" and when you click on second marker first marker info window will be dismissed and second marker info window will be shown with title "KIEL" check once and update me – NARESH REDDY May 01 '14 at 12:17
  • yes i Have done this... It showing onclick of marker ... But I want that both marker will show info window when map will open automatically ....not on click – Ghost Hacker May 01 '14 at 12:20
  • You can show info window of marker by using marker.showInfoWindow() method. But i think it will show only one infowindow – NARESH REDDY May 01 '14 at 12:28
  • yes dear I have Written this that i used this and its working on only one marker :-( – Ghost Hacker May 01 '14 at 12:32
  • As i said we can show only one infowindow at a time. Once check this question http://stackoverflow.com/questions/15899619/opening-infowindow-automatically-when-adding-marker-google-maps-v2-android – NARESH REDDY May 01 '14 at 12:34
  • @NARESHREDDY , I have used same as you suggest, but it shows same name in both case either I have set different title – Kimmi Dhingra Dec 03 '14 at 07:55