Android Google Maps API v2 - how to change marker icon

依然范特西╮ 提交于 2021-02-05 14:45:19

问题


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

回答1:


// 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




回答2:


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



回答3:


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



回答4:


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)



回答5:


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



回答6:


Inside "onMapReady(GoogleMap googleMap)"

MarkerOptions marker = new MarkerOptions();
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable._icon));



回答7:


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



回答8:


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


来源:https://stackoverflow.com/questions/18486503/android-google-maps-api-v2-how-to-change-marker-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!