I am trying to load image in Google map that I have fetched from Firebase Storage in marker window. The thing is, when I try to load it with Glide or Picasso, it's not loading in Google map. Every log I have in the code is visible in logcat. Am I doing something wrong here?
Here is my code
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;
}
}
Here is my XML file:
<?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>
The problem is that Map markers are rendered as Bitmaps and are not live Views, which means that by the time Picasso finishes loading the image, you're marker has already been rendered and you need to refresh it to display the changes.
What you need to do is implement a Callback on the, either Picasso, or Glide call and call marker.showInfoWindow()
once the image is loaded.
EDIT: I think that this is the only change that you need:
Picasso.with(context).load(uri).into(img, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
marker.showInfoWindow()
}
@Override
public void onError() {
}
});
You could also check if (marker != null && marker.isInfoWindowShown())
before calling marker.showInfoWindow()
...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments