繁体   English   中英

自定义地图标记

[英]Custom Map Marker

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

这是我创建标记的地方:

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);
            }
        });

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

 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) ;

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM