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 the app I get java.lang.NullPointerException
.
File file = new File("J:\\!!! DOCUMENTS\\!Outsourcing\\AppStore\\Benzinostancii\\Petrol\\logo.png");
Bitmap bit = BitmapFactory.decodeFile(String.valueOf(file));
double Dlat = lat.get(index);
double Dlon = lon.get(index);
String info = Arrayinfo.get(index);
String name = Arrayname.get(index);
LatLng coordinate = new LatLng(Dlat, Dlon);
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(bit))
.position(coordinate)
.title(info)
).setSnippet(name);
// 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);
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));
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);
}
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)
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);
Inside "onMapReady(GoogleMap googleMap)"
MarkerOptions marker = new MarkerOptions();
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable._icon));
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);
val marker = googleMap?.addMarker {
position(LatLng(24.774265, 46.738586))
title("Saudi Arabia")
draggable(true)
icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_address_location))
}
mMap = googleMap;
LatLng sydney = new LatLng(Lat,Lng);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,12.0f));
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.