简体   繁体   中英

Custom Map Marker

I have a map view with markers. I'm wanting to know if I can add a couple of other string values to the markers such as phone number and website. I don't want these to show in the info window when a marker is tapped. When the info window is tapped it goes to a detail activity for the selected marker. The marker title and snippet are passed as extras to the detail activity and I'd like to pass the two additional strings as extras as well.

Here is where I create the markers:

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

And here is where I start the detail activity.

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

I created custom marker with the following code please check whether it's helpful to you

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

method for customizing marker

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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