Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中在mapview的覆盖项上绘制唯一标签_Android_Google Maps_Overlayitem - Fatal编程技术网

如何在android中在mapview的覆盖项上绘制唯一标签

如何在android中在mapview的覆盖项上绘制唯一标签,android,google-maps,overlayitem,Android,Google Maps,Overlayitem,这是我试图实现的一个截图 我想为地图的每个覆盖项目添加唯一的标签(如数字)。我已经完成了添加覆盖项并在地图上显示它们的基本部分。但这是我长期以来一直困扰的地方您可以使用以下函数在同一个项目化overlay上添加带有不同标记的覆盖项目: overlayItem.setMarker(drawable); 要使其正常工作,您需要在可绘制上设置边界: Drawable icon1 = getResources().getDrawable(R.drawable.icon1); Drawable ico

这是我试图实现的一个截图


我想为地图的每个覆盖项目添加唯一的标签(如数字)。我已经完成了添加覆盖项并在地图上显示它们的基本部分。但这是我长期以来一直困扰的地方

您可以使用以下函数在同一个
项目化overlay
上添加带有不同标记的
覆盖项目

overlayItem.setMarker(drawable);
要使其正常工作,您需要在
可绘制
上设置边界:

Drawable icon1 = getResources().getDrawable(R.drawable.icon1);
Drawable icon2 = getResources().getDrawable(R.drawable.icon2);
icon1.setBounds(0, 0, icon1.getIntrinsicWidth(), icon1.getIntrinsicHeight());
icon2.setBounds(0, 0, icon2.getIntrinsicWidth(), icon2.getIntrinsicHeight());
OverlayItem item1 = new OverlayItem(new Point(48858290, 2294450), 
    "Tour Eiffel", "La tour Eiffel");
OverlayItem item2 = new OverlayItem(new Point(48873830, 2294800), 
    "Arc de Triomphe", "L'arc de triomphe");                        
item1.setMarker(icon1);
item2.setMarker(icon2);
您将需要尽可能多的位图作为标记的最大数量。但它比在位图上动态绘制文本要快。这是一部手机,处理器速度不快。 如果您喜欢在位图上绘制文本,这非常简单,您可以这样做:

//get a reference on the ImageView 
ImageView iv = (ImageView)findViewById(R.id.myImage);

// load the marker image
Bitmap myRefBitmap = BitmapFactory.decodeResource(getResources(), 
    R.drawable.icon);

// create a mutable bitmap with the same size as the marker image
Bitmap myWrittenBitmap = Bitmap.createBitmap(myRefBitmap.getWidth(), 
    myRefBitmap.getHeight(), Bitmap.Config.ARGB_4444);

// create a Canvas on which to draw and a Paint to write text.
Canvas canvas = new Canvas(myWrittenBitmap);
Paint txtPaint = new Paint();
txtPaint.setColor(Color.RED);
txtPaint.setTextSize(12);
txtPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
txtPaint.setTypeface(Typeface.DEFAULT_BOLD);

//draw ref bitmap then text on our canvas
canvas.drawBitmap(myRefBitmap, 0, 0, null);
canvas.drawText("Droid", 5, 15, txtPaint);

// set the new written bitmap into the ImageView
iv.setImageBitmap(myWrittenBitmap);

但我只想在标记上添加标签。我可以有100多个标记。添加这么多标记作为资源似乎不是一个好主意。我编辑了答案并添加了在位图上绘制文本的方法。但我真的认为,在移动平台上使用预编译的资源比动态生成的资源要好。感谢您的回复,但正如您所建议的那样,从可绘制资源创建位图和从可绘制资源创建位图的成本非常高。相反,我想重写ItemizeOverlay类的onDraw方法,遍历它的所有项,然后相应地绘制文本。一旦生成位图,默认的onDraw行为将使用缓存,如果您真的想自己绘制文本,最好使用onCreate方法。