尝试使用 android studio 添加图片标记 google 地图

Try to add a picture marker google maps with android studio

我试图在标记 google 地图中添加 drawable 但它不起作用。 我遵循了很多教程并尝试了很多东西但都没有成功...... 谁能帮帮我?

这是我的代码:

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

这对我有用:

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

你必须先把它改成BitmapDrawable,然后再转换成Bitmap,然后你才能把图片加到marker,像这样:

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

希望对您有所帮助。