我正在尝试从URL加载标记图标,但它没有向我显示地图上的图标。我做错了什么。当我使用Bitmap加载图标时,它正在工作。
毕加索图像加载对我不起作用。
我在日志中得到了项目名称:
> ....
D /成功:@ +为Duke Of Wellington设置位图PT尺寸:#3
D /成功:@ +为Dante Gabriel Rossetti设置位图PT尺寸:#2
D /成功:@ +设置Pierre Teilhard de Chardin的位图PT大小:#1
这是我的完整代码:
private class CreateProductListTask extends AsyncTask<Void, Void, List<Product>> {
private String serverUrl;
public CreateProductListTask(String url) {
super();
this.serverUrl = url;
}
@Override
protected List<Product> doInBackground(Void... params) {
.....
JSONObject response = new JSONObject(stringBuffer.toString());
List<Product> products = new ArrayList<>();
HashMap<String, Bitmap> iconsMap = new HashMap<>();
try {
JSONArray productsJSON = response.getJSONArray("all_products");
for (int ixProduct = 0; ixProduct < productsJSON.length(); ixProduct++) {
JSONObject productJSON = productsJSON.getJSONObject(ixProduct);
String mapIconStr = productJSON.getString("map_icon");
URI uri = new URI(mapIconStr);
String[] segments = uri.getPath().split("/");
String iconName = segments[segments.length - 1];
// percetn-encode URL
String mapIconPath = mapIconStr.substring(0, mapIconStr.indexOf(iconName));
String iconUrlString = mapIconPath + URLEncoder.encode(iconName, "UTF-8");
// replace "http:" with "https:"
iconUrlString = iconUrlString.replace("http:", "https:");
try {
Product product = new Product();
product.id = productJSON.getString("ID");
product.name = productJSON.getString("post_title");
product.lat = productJSON.getDouble("latitude");
product.lon = productJSON.getDouble("longitude");
id = product.id;
System.out.println("my Id stored" + id);
product.icons= iconUrlString;
products.add(product);
} catch (Exception ignore) {
}
}
} catch (JSONException ex) {
Log.e("App", "Failure", ex);
}
return products;
} catch (Exception ex) {
Log.e("App", "yourDataTask", ex);
return null;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onPostExecute(List<Product> products) {
if (products != null) {
PoiTarget pt;
for (final Product product : products) {
Marker marker = googlemap.addMarker(new MarkerOptions()
.position(new LatLng(product.lat, product.lon))
.title(product.name)
/* .icon(BitmapDescriptorFactory.fromBitmap(product.icon))*/);
pt = new PoiTarget(marker);
poiTargets.add(pt);
Picasso.with(Frnt_mapActivity.this)
.load(product.icons)
.into(pt);
markerIds.put(marker, product.id);
}
}
}
}
//--------------------------------------------------------
// Inner class
//--------------------------------------------------------
class PoiTarget implements Target {
private Marker m;
public PoiTarget(Marker m) { this.m = m; }
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
m.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
poiTargets.remove(this);
Log.d("success"," @+ Set bitmap for "+m.getTitle()+" PT size: #"+poiTargets.size());
}
@Override public void onBitmapFailed(Drawable errorDrawable) {
Log.e("Load Image Failed"," @+ [ERROR] Don't set bitmap for "+m.getTitle());
poiTargets.remove(this);
}
@Override public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
答案 0 :(得分:0)
为此目的使用Glide,它比毕加索更快地加载位图。有时毕加索在加载时会抛出错误,但在Glide中这种情况不会发生。
也不要忘记在加载图像后关闭并重新打开标记。请参阅下面的代码。
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
marker.showInfoWindow();
}
如果您仍然感到困惑,请告诉我。我将发布我的完整代码。
答案 1 :(得分:-1)
替换此代码
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
m.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
poiTargets.remove(this);
Log.d("success"," @+ Set bitmap for "+m.getTitle()+" PT size: #"+poiTargets.size());
}
与
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
poiTargets.remove(this);
m.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
Log.d("success"," @+ Set bitmap for "+m.getTitle()+" PT size: #"+poiTargets.size());
}