Android Google Maps API v2 - how to change marker icon

后端 未结 8 1147
青春惊慌失措
青春惊慌失措 2021-01-31 16:34

I am trying to change marker icon. I get the image from one server directory.

When I put break point every time the \"bit\" result is null. And when I run t

相关标签:
8条回答
  • 2021-01-31 16:48

    Try this,

      BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.location);
                LatLng bangalore = new LatLng(12.9716, 77.5946);
    
     MarkerOptions markerOptions = new MarkerOptions().position(bangalore)
              .title("Current Location")
                .snippet("hello").icon(icon);
    
    
     mMap.addMarker(markerOptions);
    
    0 讨论(0)
  • 2021-01-31 16:51

    It's very simple :

    new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.icon))
    

    In case you want the icon from a bitmap

    new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap));
    
    0 讨论(0)
  • 2021-01-31 16:52
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;
    
    // create marker
    MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
    
    // Changing marker icon
    marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));
    
    // adding marker
    googleMap.addMarker(marker);
    

    More Info

    0 讨论(0)
  • 2021-01-31 16:56

    Use .icon() Add like this

    Marker marker = map.addMarker(new MarkerOptions().position(currentLocation)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_place_holder)));
    

    Please note don't use vector Images, if you want to use vector use below code

    private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes  int vectorDrawableResourceId) {
        Drawable background = ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp);
        background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
        Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId);
        vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
        Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        background.draw(canvas);
        vectorDrawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
    
    0 讨论(0)
  • 2021-01-31 16:56

    In the newer versions of GoogleMaps for Android you can't call .setIcon() on a MarkerOptions object. Instead you have to add the marker options to the map which will give you a Marker on which you can then change the icon.

    In Kotlin the code would look like this:

    val markerOptions = MarkerOptions()
    markerOptions.position(LatLng(40.419900, -111.880767))
    val marker: Marker?  = googleMap?.addMarker(markerOptions)
    val bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.marker_customer_symbol)
    marker?.setIcon(bitmapDescriptor)
    
    0 讨论(0)
  • 2021-01-31 17:04

    For Xamarin C# users:

    tappedMarker.Remove();
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.SetTitle(tappedMarker.Title);
    markerOptions.SetPosition(tappedMarker.Position);
    
    markerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen));
    tappedMarker = googleMap.AddMarker(markerOptions);
    
    0 讨论(0)
提交回复
热议问题