从URL谷歌地图Android API在信息窗口中显示图像
我在导航抽屉里的碎片里有一张地图。现在,地图的代码可以在导航抽屉的MainActivity.java中找到。我已经有一个for循环在我的地图上了。现在,for循环的每次迭代都从Firebase获取检索到的数据,以便能够锁定标记。检索到的数据还包含图像的URL,我需要使用这些URL在每个标记的信息窗口中显示图像。我试图理解提供的其他解决方案,但我不知道如何实现这一点 这是到目前为止我的代码我的代码:从URL谷歌地图Android API在信息窗口中显示图像,android,google-maps-markers,google-maps-android-api-2,Android,Google Maps Markers,Google Maps Android Api 2,我在导航抽屉里的碎片里有一张地图。现在,地图的代码可以在导航抽屉的MainActivity.java中找到。我已经有一个for循环在我的地图上了。现在,for循环的每次迭代都从Firebase获取检索到的数据,以便能够锁定标记。检索到的数据还包含图像的URL,我需要使用这些URL在每个标记的信息窗口中显示图像。我试图理解提供的其他解决方案,但我不知道如何实现这一点 这是到目前为止我的代码我的代码: @Override public void onMapReady(GoogleMap google
@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()));
}
}
编辑
我尝试按如下方式实现它,但infowindow是空的;不显示文本视图或图像视图
@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>
对于不同的图像,您需要为地图中的不同标记创建一个imageURL的数组列表。我已经尝试过了,但是信息窗口没有任何背景,图像也没有显示,ic也没有启动。其他textview的呢?是的,textview出现了,但您是否在清单中添加了
?我的代码。它没有得到图像的URL,但现在我修复了它。但是infowindow仍然不能正常工作。我编辑了这篇文章,加入了一个截图。这两个标记都显示了相同的照片;两个标记都有与之关联的不同图像URL。而且现在看不到文本视图