【问题标题】:Customised Infowindow not working?自定义信息窗口不起作用?
【发布时间】:2017-08-24 09:41:57
【问题描述】:

我正在尝试自定义我的 Infowindow,但我无法将 TextViews 与字符串绑定。 InfoWindow 有时会显示原始 xml,有时只是无法正常工作。我觉得自己有点失落。

加载功能似乎工作正常。在我看来,问题始于 mMap.addMarker 函数。

private void placeAllMarkers() {

    mContext = getActivity();
    mPrefManager = new PrefManager(mContext);
    List <MapMarker> allMapMarker;

    allMapMarker = mPrefManager.loadAllMapMarker();

    if (allMapMarker.size() > 0) {

        for (int counter = 0; counter < allMapMarker.size(); counter++) {

            price = allMapMarker.get(counter).getSum();
            shop = allMapMarker.get(counter).getShop();
            date = allMapMarker.get(counter).getDate();
            Float GPS_X = allMapMarker.get(counter).getGPS_X();
            Float GPS_Y = allMapMarker.get(counter).getGPS_Y();

            LatLng Position = new LatLng(GPS_X, GPS_Y);

            mMap.addMarker(new MarkerOptions()
                    .position(Position)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                @Override
                public View getInfoWindow(Marker marker) {
                    InfoView = getActivity().getLayoutInflater().inflate(R.layout.custom_infowindow, null);

                    setShop = (TextView) InfoView.findViewById(R.id.setShop);
                    setSum = (TextView) InfoView.findViewById(R.id.setSum);
                    setDate = (TextView) InfoView.findViewById(R.id.setDate);

                    setShop.setText(shop);
                    setSum.setText(price);
                    setDate.setText(date);

                    return InfoView;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    return null;
                }
            });

        }
    }
}

感谢您的宝贵时间。

编辑

这是字符串的东西:

致命异常:主要 进程:hu.mosomaci.flagsproject,PID:4983 android.content.res.Resources$NotFoundException:字符串资源 ID #0xd98 在 android.content.res.Resources.getText(Resources.java:252) 在 android.content.res.MiuiResources.getText(MiuiResources.java:108) 在 android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 在 android.widget.TextView.setText(TextView.java:3895) 在 hu.mosomaci.flagsproject.fragments.MapForFlags$1.getInfoContents(MapForFlags.java:136) 在 com.google.android.gms.maps.GoogleMap$7.zzi(未知来源) 在 com.google.android.gms.maps.internal.zzd$zza.onTransact(未知来源) 在 android.os.Binder.transact(Binder.java:361)

【问题讨论】:

    标签: android


    【解决方案1】:

    首先像下面这样设置你的标记。为价格和日期设置 sn-p,并在它们之间添加 % 符号。

    mMap.addMarker(new MarkerOptions()
    .position(Position)  
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    .title(shop)
    .snippet(price + "%"+date)); 
    

    尝试在getInfoContents(Marker marker)方法内设置文本和其他工作(因为getInfoContents(Marker marker)用于定义内容。)而不是setInfoWindowAdaptergetInfoWindow(Marker marker)

    当设置文本到 TextView 时,拆分 sn-p 并通过索引获取价格和日期

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    
                    @Override
                    public View getInfoWindow(Marker marker) {
                   return null;
                    }
    
                    @Override
                    public View getInfoContents(Marker marker) {
                        InfoView = getActivity().getLayoutInflater().inflate(R.layout.custom_infowindow, null);
    
                        TextView setShop = (TextView) InfoView.findViewById(R.id.setShop);
                        TextView setSum = (TextView) InfoView.findViewById(R.id.setSum);
                        TextView setDate = (TextView) InfoView.findViewById(R.id.setDate);
    
                        String snippet = marker.getSnippet(); 
                        String[] splited_text = snippet.split("%"); 
                        String price = splited_text[0]; 
                        String date = splited_text[1];
    
                        setShop.setText(marker.getTitle());
                        setSum.setText(price);
                        setDate.setText(date);
    
                        return InfoView;
                    }
                });
    

    【讨论】:

    • 我使用了你的编辑并用这个实现了,现在它可以工作了,但是价格和日期都包含。 setShop.setText(marker.getTitle()); setSum.setText(marker.getSnippet()); setDate.setText(marker.getSnippet());
    • 我可以再添加一个单独的sn-p吗?
    • 你可以在snipet中使用字符串或解析html
    • 我只有 2 个字段,我可以处理(标题和 sn-p),但我有 3 个字符串。 (商店、价格、日期)。在这种情况下我该怎么办?
    • 大声笑,你是个策划者。这是工作。您能否编辑您的答案以使其 100% 清楚?那我会接受的。顺便谢谢你的想法:)
    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    相关资源
    最近更新 更多