How to open a Infowindow on every marker (multiple

2020-06-22 03:05发布

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);
    }
}

标签: android
3条回答
放我归山
2楼-- · 2020-06-22 03:15

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.

查看更多
老娘就宠你
3楼-- · 2020-06-22 03:25

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

查看更多
劫难
4楼-- · 2020-06-22 03:29

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.

查看更多
登录 后发表回答