如何在android中的每个标记(多标记)上打开Infowindow?

时间:2014-05-01 11:49:06

标签: android

由于我使用谷歌地图v2,我想打开多个制造商的信息窗口。我用单个标记完成了这个,并且还使用了show info window();但它适用于最后一个标记而且仅适用于一个标记。 我只想在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);
    }
}

3 个答案:

答案 0 :(得分:9)

默认标记一次只能显示一个信息窗口。但是,您可以使用地图Android库轻松显示更多内容:https://developers.google.com/maps/documentation/android-api/utility/

以下是一个例子:

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

并添加

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

到你的gradle文件。

答案 1 :(得分:2)

根据谷歌地图Android v2

信息窗口允许您在用户点击地图上的标记时向用户显示信息。默认情况下,如果标记具有标题集,则当用户点击标记时会显示信息窗口。一次只显示一个信息窗口。如果用户单击另一个标记,则将隐藏当前窗口并显示新的信息窗口。您可以通过在目标标记上调用showInfoWindow()以编程方式显示信息窗口。可以通过调用hideInfoWindow()

隐藏信息窗口

喜欢:

class ProviderFactory
{

    public function createProvider($typeId)
    {
        if ($typeId == 2) {
            return new Provider2($arg1, $arg5);
        } elseif ($typeId == 4) {
            return new Provider4();
        } else {
            return new ProviderDefault($typeId, $arg1, $arg2, $arg3, $arg4);
        }
    }
}


interface ProviderInterface
{
    public function getCost();
}

class ProviderDefault implements ProviderInterface
{
    public function __construct($arg1, $arg2, $arg3, $arg4) {}

    public function getCost() { /*implementation*/ }
}

class Provider2 implements ProviderInterface
{
    public function __construct($arg1, $arg5) {}

    public function getCost() { /*implementation*/ }
}

// this call can be implemented with the following condition
// if ($typeId == 2) {
//      if ($provider2->getCost() !== null)
//          $typeId = 1;
// }
//
$typeId = fixAndValidateTypeId($typeId, new Provider2($arg1, $arg5));


$factory = new ProviderFactory();
$provider = $factory->createProvider($typeId);

并使用地图指针重复此操作。

答案 2 :(得分:0)

首先评论这一行。

googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

然后使用此代码在地图上添加标记

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

然后评论其他代码并执行一次。然后我将给出CustomAdapter代码