37

我正在尝试更改标记图标。我从一个服务器目录中获取图像。

当我每次设置断点时,“位”结果都是null. 当我运行应用程序时,我得到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);
4

8 回答 8

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

更多信息

于 2013-12-09T07:27:32.670 回答
19

这很简单:

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

如果您想要位图中的图标

new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap));
于 2017-04-02T13:27:51.573 回答
14

像这样使用.icon() 添加

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

请注意不要使用矢量图像,如果你想使用矢量使用下面的代码

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);
}
于 2019-01-30T07:47:09.920 回答
11

在 Android 版 GoogleMaps 的较新版本中,您不能调用.setIcon()对象MarkerOptions。相反,您必须将标记选项添加到地图中,这将为您提供一个Marker然后您可以在其上更改图标。

在 Kotlin 中,代码如下所示:

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)
于 2018-09-27T13:59:27.393 回答
1

对于 Xamarin C# 用户:

tappedMarker.Remove();
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.SetTitle(tappedMarker.Title);
markerOptions.SetPosition(tappedMarker.Position);

markerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen));
tappedMarker = googleMap.AddMarker(markerOptions);
于 2017-06-15T13:09:27.717 回答
0

尝试这个,

  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);
于 2018-06-25T05:30:10.507 回答
0

在“onMapReady(GoogleMap googleMap)”里面

MarkerOptions marker = new MarkerOptions();
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable._icon));
于 2018-03-14T17:50:28.760 回答
-1
      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));
于 2018-05-04T11:53:44.693 回答