Set Image from drawable as marker in Google Map version 2

前端 未结 4 1183
难免孤独
难免孤独 2020-12-23 12:59

I am using this part of code to add a marker in a MapFragment in Google Map Version 2.

MarkerOptions op = new MarkerOptions();
op.position(point         


        
相关标签:
4条回答
  • 2020-12-23 13:37

    This is how you can set a Drawable as a Marker.

    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.current_position_tennis_ball)
    
    MarkerOptions markerOptions = new MarkerOptions().position(latLng)
             .title("Current Location")
             .snippet("Thinking of finding some thing...")
             .icon(icon);
    
    mMarker = googleMap.addMarker(markerOptions);
    

    VectorDrawables and XML based Drawables do not work with this.

    0 讨论(0)
  • 2020-12-23 13:54

    @Lukas Novak answer is not showing anything because you also have to set the bounds on Drawable.
    This works for any drawable. Here is a fully working example:

    public void drawMarker() {
        Drawable circleDrawable = getResources().getDrawable(R.drawable.circle_shape);
        BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable);
    
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(41.906991, 12.453360))
                .title("My Marker")
                .icon(markerIcon)
        );
    }
    
    private BitmapDescriptor getMarkerIconFromDrawable(Drawable drawable) {
        Canvas canvas = new Canvas();
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
    


    circle_shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
        <size android:width="20dp" android:height="20dp"/>
        <solid android:color="#ff00ff"/>
    </shape>
    
    0 讨论(0)
  • 2020-12-23 14:00

    If you have Drawable created programatically (so you have no resource for it), you can use this:

    Drawable d = ... // programatically create drawable
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    d.draw(canvas);
    BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(bitmap);
    

    Then you have BitmapDescriptor, which you can pass into MarkerOptions.

    0 讨论(0)
  • 2020-12-23 14:02

    Basically use bitmap insted of drawable. If you do have a drawable, convert it to bitmap.
    Here is a code example in kotlin.

    private fun placeNewMarker(whereToPlaceYourMarker: LatLng){
    
        val pickupMarkerDrawable = resources.getDrawable(R.drawable.your_marker_drawable,null)
    
    
        _pickupMarker =   mMap?.addMarker(MarkerOptions()
                .position(whereToPlaceYourMarker)
                .draggable(true)
                .title("My Marker")
                .icon(BitmapDescriptorFactory.fromBitmap(pickupMarkerDrawable.toBitmap(pickupMarkerDrawable.intrinsicWidth, pickupMarkerDrawable.intrinsicHeight, null))))
    
    
    
    }
    
    0 讨论(0)
提交回复
热议问题