【问题标题】:Google Map toolbar on Android does not showingAndroid 上的 Google 地图工具栏不显示
【发布时间】:2016-09-27 13:33:33
【问题描述】:

我正在使用 google maps API,并且我为标记创建了一个自定义信息窗口。

我需要显示工具栏地图,但使用这种方法

map.getUiSettings().setMapToolbarEnabled(true);

当我点击标记时,它会显示我的信息窗口,而不是工具栏。

我想在点击标记时显示工具栏。

下面是我的代码:

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.getUiSettings().setMapToolbarEnabled(true);
        mMap.setOnMarkerClickListener(this);
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                MarkerCouple idtype = (MarkerCouple) marker.getTag();

                if (idtype.getType() == ResourceType.EVENTS 
                     && currentEvents.get(idtype.getID()) != null) {

                    Resources resources = currentResources
                            .get(currentEvents.get(idtype.getID()).getRes_ID());
                    Events event = currentEvents.get(idtype.getID());

                    Intent i = new Intent(MainActivity.this, ShowEventActivity.class);
                    i.putExtra(Variable.USER_JSON, user.toJSON());
                    i.putExtra(Variable.EVENT_JSON, event.toJSON());
                    i.putExtra(Variable.RESOURCE_JSON, resources.toJSON());

                    startActivityForResult(i, Variable.SHOW_EVENT);
                }

                if (idtype.getType() == ResourceType.PLACES
                     && currentPlaces.get(idtype.getID()) != null) {
                    Resources resources = currentResources
                            .get(currentPlaces.get(idtype.getID()).getRes_ID());
                    Places place = currentPlaces.get(idtype.getID());

                    Intent i = new Intent(MainActivity.this, ShowPlaceActivity.class);
                    i.putExtra(Variable.USER_JSON, user.toJSON());
                    i.putExtra(Variable.PLACE_JSON, place.toJSON());
                    i.putExtra(Variable.RESOURCE_JSON, resources.toJSON());

                    startActivityForResult(i, Variable.SHOW_PLACE);
                }
            }
        });

        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.marker_preview, null);
                ImageView m = (ImageView) v.findViewById(R.id.marker_image);
                m.setImageBitmap(Bitmap.createScaledBitmap(imageToShow, 600, 400, true));
                return v;
            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;
            }
        });
        new MarkerAsync().execute();
    }

    @Override
    public boolean onMarkerClick(final Marker marker) {
        MarkerCouple idtype = (MarkerCouple) marker.getTag();

        if (idtype.getType() == ResourceType.EVENTS && currentEvents.get(idtype.getID()) != null) {
            Events event = currentEvents.get(idtype.getID());
            final Resources resources = currentResources.get(event.getRes_ID());
            Picasso.with(MainActivity.this).invalidate(Variable.DOWNLOAD_URL + resources.getURI());
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        imageToShow = Picasso.with(MainActivity.this).load(Variable.DOWNLOAD_URL + resources.getURI()).get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void v) {
                    marker.showInfoWindow();
                }
            }.execute();
        }


        if (idtype.getType() == ResourceType.PLACES && currentPlaces.get(idtype.getID()) != null) {
            Places place = currentPlaces.get(idtype.getID());
            final Resources resources = currentResources.get(place.getRes_ID());
            Picasso.with(MainActivity.this).invalidate(Variable.DOWNLOAD_URL + resources.getURI());
            Picasso.with(MainActivity.this).invalidate(Variable.DOWNLOAD_URL + resources.getURI());
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        imageToShow = Picasso.with(MainActivity.this).load(Variable.DOWNLOAD_URL + resources.getURI()).get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void v) {
                    marker.showInfoWindow();
                }
            }.execute();
        }
        return true;
    }

【问题讨论】:

  • 请发布您的代码

标签: android google-maps maps toolbar


【解决方案1】:

只需return false; 来自您的onMarkerClick()

来自the documentation

public abstract boolean onMarkerClick(标记标记)

退货

如果侦听器已经消费了事件,则为 true(即不应发生默认行为),否则为 false(即应发生默认行为)。默认行为是让相机移动到标记处并显示一个信息窗口。

【讨论】:

  • 如果我返回 false,我会抛出异常
  • 有什么异常?
  • java.lang.NullPointerException:尝试在 android.graphics.Bitmap.createScaledBitmap(Bitmap.java:591) 的空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()” ) 在 tesi.myview.MainActivity$3.getInfoWindow(MainActivity.java:274)
  • 当您的getInfoWindow 被调用时,您的imageToShow 似乎为空
  • 我只需要工具栏的默认行为,因为我需要显示我的自定义信息窗口
猜你喜欢
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多