图片未加载到 Google 地图标记中
Image not load in Google map marker
我正在尝试在 Google 地图中加载我从标记 window 中的 Firebase 存储中获取的图像。问题是,当我尝试使用 Glide 或 Picasso 加载它时,它没有加载到 Google 地图中。我在代码中的每个日志都在 logcat 中可见。我是不是做错了什么?
这是我的代码
public class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
Context context;
LayoutInflater inflater;
private DatabaseReference mDatabase;
private StorageReference storageReference;
private Chalet chalet;
private boolean query;
private ImageView img;
private String lat;
private String log;
public MapInfoWindowAdapter(Context context, LayoutInflater inflater) {
this.context = context;
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(final Marker marker) {
mDatabase = FirebaseDatabase.getInstance().getReference().child("chalets");
storageReference=FirebaseStorage.getInstance().getReference();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.marker_tag,null);
final LatLng latlng = marker.getPosition();
Double doubleLat = latlng.latitude;
lat = String.valueOf(latlng.latitude);
log = String.valueOf(latlng.longitude);
Log.i("MarkerPostion",lat);
img = (ImageView) v.findViewById(R.id.chaletImg);
// final TextView textView = (TextView) v.findViewById(R.id.markerText);
final Semaphore semaphore = new Semaphore(0);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("MarkerPostion","112211");
for (DataSnapshot snapm: dataSnapshot.getChildren()) {
Log.i("MarkerPostion",snapm.child("latitude").getValue().toString());
Log.i("Key",snapm.getKey());
if ( lat.equals(snapm.child("latitude").getValue().toString()) && log.equals(snapm.child("longitude").getValue().toString()) ){
chalet = snapm.getValue(Chalet.class);
Log.i("MarkerPostion","9999");
break;
}
}
storageReference = FirebaseStorage.getInstance().getReference().child(chalet.getOwnerID()).child(chalet.getChaletNm()).child("1");
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(context).load(uri).into(img);
Log.i("MarkerPostion","2222");
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return v;
}
}
这是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/markerFrame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/chaletImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
</LinearLayout>
</FrameLayout>
尝试使 xml 中的 LinearLayout
成为布局的根视图。我从来没有意识到为什么,但是当我处理没有明显 explanation/bug 的自定义 InfoWindowAdapter
时,我也遇到了问题。使用 LinearLayout
作为 xml 的根视图解决了我的问题。
问题是地图标记被渲染为位图而不是实时视图,这意味着当 Picasso 完成加载图像时,您的标记已经被渲染,您需要刷新它以显示变化。
您需要做的是在 Picasso 或 Glide 调用上实现回调,并在加载图像后调用 marker.showInfoWindow()
。
编辑:
我认为这是您唯一需要的更改:
Picasso.with(context).load(uri).into(img, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
marker.showInfoWindow()
}
@Override
public void onError() {
}
});
您还可以在调用 marker.showInfoWindow()
之前检查 if (marker != null && marker.isInfoWindowShown())
...
我正在尝试在 Google 地图中加载我从标记 window 中的 Firebase 存储中获取的图像。问题是,当我尝试使用 Glide 或 Picasso 加载它时,它没有加载到 Google 地图中。我在代码中的每个日志都在 logcat 中可见。我是不是做错了什么?
这是我的代码
public class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
Context context;
LayoutInflater inflater;
private DatabaseReference mDatabase;
private StorageReference storageReference;
private Chalet chalet;
private boolean query;
private ImageView img;
private String lat;
private String log;
public MapInfoWindowAdapter(Context context, LayoutInflater inflater) {
this.context = context;
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(final Marker marker) {
mDatabase = FirebaseDatabase.getInstance().getReference().child("chalets");
storageReference=FirebaseStorage.getInstance().getReference();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.marker_tag,null);
final LatLng latlng = marker.getPosition();
Double doubleLat = latlng.latitude;
lat = String.valueOf(latlng.latitude);
log = String.valueOf(latlng.longitude);
Log.i("MarkerPostion",lat);
img = (ImageView) v.findViewById(R.id.chaletImg);
// final TextView textView = (TextView) v.findViewById(R.id.markerText);
final Semaphore semaphore = new Semaphore(0);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("MarkerPostion","112211");
for (DataSnapshot snapm: dataSnapshot.getChildren()) {
Log.i("MarkerPostion",snapm.child("latitude").getValue().toString());
Log.i("Key",snapm.getKey());
if ( lat.equals(snapm.child("latitude").getValue().toString()) && log.equals(snapm.child("longitude").getValue().toString()) ){
chalet = snapm.getValue(Chalet.class);
Log.i("MarkerPostion","9999");
break;
}
}
storageReference = FirebaseStorage.getInstance().getReference().child(chalet.getOwnerID()).child(chalet.getChaletNm()).child("1");
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(context).load(uri).into(img);
Log.i("MarkerPostion","2222");
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return v;
}
}
这是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/markerFrame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/chaletImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
</LinearLayout>
</FrameLayout>
尝试使 xml 中的 LinearLayout
成为布局的根视图。我从来没有意识到为什么,但是当我处理没有明显 explanation/bug 的自定义 InfoWindowAdapter
时,我也遇到了问题。使用 LinearLayout
作为 xml 的根视图解决了我的问题。
问题是地图标记被渲染为位图而不是实时视图,这意味着当 Picasso 完成加载图像时,您的标记已经被渲染,您需要刷新它以显示变化。
您需要做的是在 Picasso 或 Glide 调用上实现回调,并在加载图像后调用 marker.showInfoWindow()
。
编辑: 我认为这是您唯一需要的更改:
Picasso.with(context).load(uri).into(img, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
marker.showInfoWindow()
}
@Override
public void onError() {
}
});
您还可以在调用 marker.showInfoWindow()
之前检查 if (marker != null && marker.isInfoWindowShown())
...