无法将图标添加到标记,映射到Android
下面是我如何将标记添加到地图中无法将图标添加到标记,映射到Android,android,google-maps-android-api-2,Android,Google Maps Android Api 2,下面是我如何将标记添加到地图中 map.addMarker(new MarkerOptions() .position(model.getLatLongfromService()) .title(model.getCoupon_name()) .snippet(model.getCoupon_id()) .icon(BitmapDescr
map.addMarker(new MarkerOptions()
.position(model.getLatLongfromService())
.title(model.getCoupon_name())
.snippet(model.getCoupon_id())
.icon(BitmapDescriptorFactory.fromFile(DataHolder.imageUrl
+ model.getCoupon_image())));
- 我将获得以下格式的优惠券图片:**
- 当你运行我的应用程序时,我遇到此错误
Rakesh我认为问题在于BitmapDescriptorFactory.fromFile方法使用参数字符串fileName,它表示要加载的文件(图像)的名称。 您提供了图像的http url(),而不是它 您可能需要先下载图像,然后使用BitmapDescriptorFactory.fromBitmap 编辑: 要下载映像,您可以使用一些异步任务,例如:
AsyncTask<String, Void, Bitmap> loadImageTask = new AsyncTask<String, Void, Bitmap>(){
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bmImg = null;
try {
URL url = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
bmImg = null;
}
return bmImg;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
// TODO: do what you need with resulting bitmap - add marker to map
}
};
谢谢你的回复。您能建议我如何从URL下载图像并使用Asyntask将其存储到位图中吗。我读了一些我需要使用asyntask的地方。
loadImageTask.execute(new String[]{yourImageUrl});