Add an Image from Url into Custom Infowindow Google Maps V2

Insert image into Google Maps infoWindow via URL

To display an image in HTML you need to pass in the URL.

   google.maps.event.addListener(marker, "click", function(e)  {
bigPic = JSON.stringify(item.photo_file_url);
var bp = bigPic.replace("mini_square","medium");
var bpnq = bp.replace(/"/g,"");
pix_Nfo.setContent('<p><img src='+bpnq+'></p>');
pix_Nfo.open(pixMap, marker);
});

proof of concept fiddle

// function initialize() {pixMap = new google.maps.Map(document.getElementById('map'));

var thumbUrl = "http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=100&minx=-2.494046&miny=53.429721&maxx=-2.00048&maxy=53.528126&size=mini_square&mapfilter=true&callback=?";var pix_Nfo = new google.maps.InfoWindow({ maxWidth: 400});
$.getJSON(thumbUrl, function(data) { var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < data.photos.length; i++) { var item = data.photos[i]; var latLng = new google.maps.LatLng(item.latitude, item.longitude); bounds.extend(latLng); var marker = new google.maps.Marker({ position: latLng, icon: item.photo_file_url }); marker.setMap(pixMap);
(function(marker, item) { google.maps.event.addListener(marker, "click", function(e) { bigPic = JSON.stringify(item.photo_file_url); var bp = bigPic.replace("mini_square", "medium"); //pix_Nfo.setContent('<img src=bp>'); var bpnq = bp.replace(/"/g, ""); pix_Nfo.setContent('<p><img src=' + bpnq + '></p>'); pix_Nfo.open(pixMap, marker); // reopen infowindow, so fits in map. google.maps.event.addListener(pix_Nfo, 'domready', function() { google.maps.event.trigger(marker, 'click'); }); }); })(marker, item); }; pixMap.fitBounds(bounds);});// }// google.maps.event.addDomListener(window,'load',initialize);
body,html,#map {  width: 100%;  height: 100%;}
<script src="https://maps.googleapis.com/maps/api/js"></script><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="map" style="border: 2px solid #3872ac;"></div>

Loading an image from an URL in to customInfoWindow Google Maps

Try removing the Picasso's

.fit().centerCrop()

Control the image view size, scaling attributes via XML, where you defined the imageView ("info_image")

How to get image in infoWindow on Google Maps with picasso - Android

The info window is basically a bitmap, captured from the views that you populated. As a result, changes to those views — such as Picasso asynchronously updating an ImageView — will not update the info window.

One solution that works is to call showInfoWindow() on the Marker after Picasso has obtained and cached the image. For example, this sample app uses Picasso to populate info windows, and uses a Picasso Callback to call showInfoWindow():

/***
Copyright (c) 2013-2014 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.

From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.mapsv2.imagepopups;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.model.Marker;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

class PopupAdapter implements InfoWindowAdapter {
private View popup=null;
private LayoutInflater inflater=null;
private HashMap<String, Uri> images=null;
private Context ctxt=null;
private int iconWidth=-1;
private int iconHeight=-1;
private Marker lastMarker=null;

PopupAdapter(Context ctxt, LayoutInflater inflater,
HashMap<String, Uri> images) {
this.ctxt=ctxt;
this.inflater=inflater;
this.images=images;

iconWidth=
ctxt.getResources().getDimensionPixelSize(R.dimen.icon_width);
iconHeight=
ctxt.getResources().getDimensionPixelSize(R.dimen.icon_height);
}

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

@SuppressLint("InflateParams")
@Override
public View getInfoContents(Marker marker) {
if (popup == null) {
popup=inflater.inflate(R.layout.popup, null);
}

if (lastMarker == null
|| !lastMarker.getId().equals(marker.getId())) {
lastMarker=marker;

TextView tv=(TextView)popup.findViewById(R.id.title);

tv.setText(marker.getTitle());
tv=(TextView)popup.findViewById(R.id.snippet);
tv.setText(marker.getSnippet());

Uri image=images.get(marker.getId());
ImageView icon=(ImageView)popup.findViewById(R.id.icon);

if (image == null) {
icon.setVisibility(View.GONE);
}
else {
Picasso.with(ctxt).load(image).resize(iconWidth, iconHeight)
.centerCrop().noFade()
.placeholder(R.drawable.placeholder)
.into(icon, new MarkerCallback(marker));
}
}

return(popup);
}

static class MarkerCallback implements Callback {
Marker marker=null;

MarkerCallback(Marker marker) {
this.marker=marker;
}

@Override
public void onError() {
Log.e(getClass().getSimpleName(), "Error loading thumbnail!");
}

@Override
public void onSuccess() {
if (marker != null && marker.isInfoWindowShown()) {
marker.showInfoWindow();
}
}
}
}

Basically, if when Picasso gets the image, the info window is still open for the associated Marker, I call showInfoWindow(). The visual effect resembles normal Picasso behavior: a placeholder, followed by the real image replacing the placeholder.

Google Maps v2: launch custom infowindow without using a marker

Is it possible to launch a custom info window in v2 without using a
marker?

It isn't, but you can create a transparent marker and show the InfoWindow on it.
To make it transparent, use a transparent resource or use marker.setAlpha(0.0f).

Which event should I use to detect a long press on the map?

You can use the method GoogleMap.setOnMapLongClickListener().

How can I launch an InfoWindow?

You have to add a Marker and call showInfoWindow() method on it.

Why Custom InfoWindow of google map v2 ,Not load url Image?

The Maps V2 UI is actually rendered by another process. You cannot modify the View you use for your info window after you pass that View to Maps V2. My guess is that you are trying to set up the View, then load the image later on, and that will not work. Get the image first, then put it into the View before you give the View to Maps V2.

This, of course, was covered in the answer that you linked to.



Related Topics



Leave a reply



Submit