自定义地图标记

时间:2015-04-01 13:31:51

标签: java android google-maps google-maps-markers marker

我有一个带标记的地图视图。我想知道我是否可以在电话号码和网站等标记中添加其他几个字符串值。当点击标记时,我不希望这些显示在信息窗口中。点击信息窗口后,它将转到所选标记的详细活动。标记标题和片段作为附加内容传递给详细活动,我也希望将两个附加字符串作为附加内容传递。

这是我创建标记的地方:

for(int i = 0; i < Lat.length; i++) {
            Marker marker = map.addMarker(new MarkerOptions()
            .position(new LatLng(Lat[i], Lon[i]))
            .title(Market[i])
            .snippet(Address[i])
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

            list.add(marker);
        }

这是我开始详细活动的地方。

map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                // Show Details
                Intent intent = new Intent(getActivity(), FarmMarketDetails.class);
                intent.putExtra("selectedTitle", marker.getTitle());
                intent.putExtra("selectedAddress", marker.getSnippet());
                startActivity(intent);
            }
        });

1 个答案:

答案 0 :(得分:1)

我使用以下代码创建了自定义标记,请检查它是否对您有帮助

 Marker marki=map.addMarker(new MarkerOptions()  
                .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.my, "")))
                 .position((new LatLng(latitude,longitude)))) ;

自定义标记

的方法
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.setColor(Color.BLUE);
        paint.setTypeface(tf);
        paint.setTextAlign(Align.CENTER);
        paint.setTextSize(convertToPixels(context,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(context,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) ;

    }