我在抽屉导航的 fragment 中有一张 map 。现在 map 的代码位于抽屉导航的 MainActivity.java 中。我在 onMapReady 中有一个 for 循环,它在我的 map 上固定标记。现在 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()));
}
}
编辑
我尝试按如下方式实现它,但信息窗口是空白的;不显示 TextView 或 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>
最佳答案
试试这个:
创建全局标记变量
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>
对于不同的图像,您需要一个包含 map 中不同标记的 imageURL 数组列表。
关于android - 在来自 URL Google Maps Android API 的信息窗口中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42885480/