[英]how to add images getting from server to google map android as a marker?
请建议我
我正在获取图片的网址,需要将其放置在Google地图中作为标记
for(AtmInfo info :atmInfoList){
//latitude = Double.parseDouble(pub.getLatitude()) ;
// longitude = Double.parseDouble(getIntent().getStringExtra("lon"));
Marker source = mMap.addMarker(
new MarkerOptions()
.position(
new LatLng(
Double.parseDouble(info.getLatitude()),
Double.parseDouble(info.getLongitude())))
.title(info.getBankName())
.snippet(info.getBankAddress())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_green)));
}
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
首先,从URL获取图像。
其次,获取图像后,您可以使用叠加层在地图上添加自定义标记。
这是完整的解决方案。 像这样创建标记
Marker source = mMap.addMarker(
new MarkerOptions()
.position(
new LatLng(
Double.parseDouble(info.getLatitude()),
Double.parseDouble(info.getLongitude())))
.title(info.getBankName())
.snippet(info.getBankAddress())
.icon(BitmapDescriptorFactory.fromResource(getBitmap(url,this)))); //call getbitmap() method to download image from url
从网址下载位图
在您的项目中添加方法getBitmap和解码方法(例如util类或通用函数)
public static Bitmap getBitmap(String url,Context context)
{
FileCache fileCache=new FileCache(context);
MemoryCache memoryCache=new MemoryCache();
File f=fileCache.getFile(url);
//from SD cache
//CHECK : if trying to decode file which not exist in cache return null
Bitmap b = decodeFile(f);
if(b!=null)
return b;
// Download image file from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
// Constructs a new FileOutputStream that writes to file
// if file not exist then it will create file
OutputStream os = new FileOutputStream(f);
// See Utils class CopyStream method
// It will each pixel from input stream and
// write pixels to output stream (file)
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
//Now file created and going to resize file with defined height
// Decodes image and scales it to reduce memory consumption
b = decodeFile(f);
return bitmap;
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
//Decodes image and scales it to reduce memory consumption
private static Bitmap decodeFile(File f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1=new FileInputStream(f);
BitmapFactory.decodeStream(stream1,null,o);
stream1.close();
//Find the correct scale value. It should be the power of 2.
// Set width/height of recreated image
final int REQUIRED_SIZE=85;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with current scale values
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
FileInputStream stream2=new FileInputStream(f);
Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
return bitmap;
} catch (FileNotFoundException e) {
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.