如何在Google地图中的一行之间显示文字

时间:2014-04-15 14:07:34

标签: android google-maps

我在google-map中显示两个点。并在两点之间划一条线。现在我有距离值,想要显示在线的中心。

我应该怎么做?

  Polyline line = mMap.addPolyline(new PolylineOptions()
  .add(geo, new LatLng(28.549961,77.4107057)) 
  .width(2)
  .color(Color.BLUE).geodesic(true)); 

上面是画线的代码

2 个答案:

答案 0 :(得分:1)

您可以使用自己设置的自定义标记。你必须找出一个算法。

答案 1 :(得分:1)

创建一个具有不可见背景的Png并将其导入您的drawable文件夹。之后使用此代码:

  obm = writeTextOnDrawable(R.drawable.text_background, yourtexthere);
LatLng point=new LatLng((latitude1+latitude2)/2,(longitude1+longitude2)/2);

    MarkerOptions markerOptions = new MarkerOptions().icon(
    BitmapDescriptorFactory.fromBitmap(obm))
    .position(point);


mMap.addMarker(markerOptions));


private Bitmap writeTextOnDrawable(int drawableId, String text) {
    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
    .copy(Bitmap.Config.ARGB_8888, true);
    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
    Paint paint = new Paint();
    paint.setStyle(Style.FILL);

    paint.setColor(Color.WHITE);

    paint.setColor(Color.BLACK);
    paint.setTypeface(font);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(convertToPixels(this, 11));
    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);
    Canvas canvas = new Canvas(bm);
    // If the text is bigger than the canvas , reduce the font size
    if (textRect.width() >= (canvas.getWidth() - 4)) // the padding on
    // either sides is
    // considered as 4,
    // so as to
    // appropriately fit
    // in the text
    paint.setTextSize(convertToPixels(this, 7)); // Scaling needs to be
    // used for
    // different dpi's
    // Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2; // -2 is for regulating the x
    // position offset
    // "- ((paint.descent() + paint.ascent()) / 2)" is the distance from the
    // baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint
    .ascent()) / 2));
    canvas.drawText(text, xPos, yPos, paint);
    return bm;
    }

    public static int convertToPixels(Context context, int nDP) {
    final float conversionScale = context.getResources()
    .getDisplayMetrics().density;
    return (int) ((nDP * conversionScale) + 0.5f);
    }

根据您的喜好编辑yourtexthere和点变量以及png资源名称