0

I tried to add a drawable in a marker google maps but it doesn't worked. I followed many tutorials and tried many things without success... Anyone can help me ?

this is my code :

I think the problem is ".icon(icon)"

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.ic_radar);

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(icon));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Maveňツ
  • 1
  • 12
  • 50
  • 89

2 Answers2

3

You have to first change it to BitmapDrawable then convert into Bitmap, then only you can add image to a marker, do it something like this:

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.icon);
    Bitmap b=bitmapdraw.getBitmap();
    Bitmap smallMarker = Bitmap.createScaledBitmap(b, 84, 84, false);

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

I hope this will help.

Maveňツ
  • 1
  • 12
  • 50
  • 89
Ashish Gupta
  • 737
  • 11
  • 18
0

This works for me :

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_radar);
    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromBitmap(icon)));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Maveňツ
  • 1
  • 12
  • 50
  • 89
SiSa
  • 2,594
  • 1
  • 15
  • 33
  • It doesn't work, the application close when it start : I have this error --> Failed sending reply to debugger: Broken pipe – Greg Constantin Sep 12 '17 at 09:42
  • @GregConstantin refer to this link for your error: https://stackoverflow.com/a/40010794/4832356 – SiSa Sep 12 '17 at 10:05