【问题标题】:Custom Map Marker自定义地图标记
【发布时间】:2015-06-06 04:22:06
【问题描述】:

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

这里是我创建标记的地方:

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

【问题讨论】:

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


    【解决方案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) ;
    
        }
    

    【讨论】:

    • 我要做的就是为每个标记设置一个电话号码和网站值,例如设置标题和 sn-p。
    • 没有。我希望一切都完全一样,除了与每个标记关联的电话号码和网站的附加字符串。我会像设置标记标题和 sn-p 一样设置这些值。然后我可以将这些值作为额外的意图传递给我的详细活动,就像我目前对标题和 sn-p 所做的那样。
    • 抱歉,我认为如果您的标记是动态的,您可以将标记详细信息保存在哈希图中,并可以在标记单击时根据标记 id(地图的位置)将其传递给附加项。
    猜你喜欢
    • 2016-09-30
    • 1970-01-01
    • 2013-08-21
    • 2015-06-06
    • 2018-01-13
    • 2014-07-18
    • 1970-01-01
    相关资源
    最近更新 更多