【问题标题】:Display image in infowindow from URL Google Maps Android API从 URL Google Maps Android API 在信息窗口中显示图像
【发布时间】:2017-08-10 15:52:36
【问题描述】:

我在导航抽屉的片段中有一张地图。现在可以在导航抽屉的 MainActivity.java 中找到地图的代码。我在 onMapReady 中有一个 for 循环,它在我的地图上固定标记。现在 for 循环的每次迭代都从 Firebase 检索数据,以便能够固定标记。检索到的数据还包含图像的 URL,我需要使用这些 URL 在每个标记的信息窗口中显示图像。我试图了解提供的其他解决方案,但我不知道如何实现这一点。

这是我的代码到目前为止我的代码:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    ...

    for (infoToStore details : info) {
        marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
        .title(details.getName())
        .snippet(details.getDesc()));

    }
}

编辑

我尝试如下实现它,但信息窗口是空白的;不显示 TextViews 和 ImageView。

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    for (final infoToStore details : info) {
        marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
        .title(details.getName())
        .snippet(details.getDesc()));

        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

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

            @Override
            public View getInfoContents(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.popup, null);

                TextView name = (TextView) v.findViewById(R.id.name);
                TextView desc = (TextView) v.findViewById(R.id.desc);
                ImageView image = (ImageView) v.findViewById(R.id.image);



                name.setText(marker.getTitle());
                desc.setText(marker.getSnippet());

                Picasso.with(getApplicationContext())
                        .load(URLString)
                        .into(image);


                return v;
            }
        });

    }
}

这是 popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/image"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/desc"/>

</LinearLayout>

【问题讨论】:

    标签: android google-maps-markers google-maps-android-api-2


    【解决方案1】:

    试试这个:

    创建一个全局标记变量

    private Marker marker;
    

    现在在您的onMapReady() 电话中

    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
    

    创建您的CustomInfoWindowAdapter 类并添加以下代码..

    private class CustomInfoWindowAdapter implements InfoWindowAdapter {
    
        private View view;
    
        public CustomInfoWindowAdapter() {
            view = getLayoutInflater().inflate(R.layout.popup,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;
    
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView desc = (TextView) view.findViewById(R.id.desc);
            ImageView image = (ImageView) view.findViewById(R.id.image);
    
            Picasso.with(getApplicationContext())
                    .load(URLString)
                    .error(R.mipmap.ic_launcher) // will be displayed if the image cannot be loaded
                    .into(image);
    
            final String title = marker.getTitle();
            if (title != null) {
                name.setText(title);
            } else {
                name.setText("Default");
            }
    
            final String snippet = marker.getSnippet();
            if (snippet != null) {
                desc.setText(snippet);
            } else {
                desc.setText("Deafult");
            }
            //getInfoContents(marker);
            return view;
        }
    }
    

    您的 imageview 非常大...这会阻止 textView:在您的弹出窗口中尝试此布局

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:gravity="center"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Sample Text"
                android:textColor="#000000"
                android:textSize="15sp" />
    
            <TextView
                android:id="@+id/desc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Sample Text2"
                android:textColor="#000000"
                android:textSize="12sp" />
    
        </LinearLayout>
    

    对于不同的图像,您需要一个用于地图中不同标记的 imageURL 数组列表。

    【讨论】:

    • 我试过了,但是信息窗口没有任何背景,图像没有显示,ic_launcher也没有显示
    • 其他 textview 的呢?
    • 是的,虽然文本视图出现了
    • 您是否在清单中添加了&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
    • 我的代码有错误。它没有获取图像的 URL,但现在我修复了它。但是信息窗口仍然无法正常工作。我编辑了帖子以包含屏幕截图。它为两个标记显示同一张照片;两个标记都有与之关联的不同图像 URL。并且 TextViews 现在无处可见
    猜你喜欢
    • 2012-05-27
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多