android 自定义覆盖物,Android集成高德地图如何自定义marker

高德地图自定义Marker

高德地图默认的marker样式是这种

b1a8a8d459419900314374d51e4bdf96.png

一般的修改样式是通过icon接口来调整

MarkerOptions markerOptions = new MarkerOptions()

.position(latlng)

.draggable(true)

.icon(BitmapDescriptorFactory.fromBitmap(bitmap));

当遇到图片加文字的marker该如何自定义呢?如这样

68317b7e76f24453375a5721fbc1ccf4.png

步骤:

/**

* 往地图上添加marker

*/

public static void addMarkersToMap(Context context, AMap aMap, LatLng latlng, PointModel model) {

if (aMap != null) {

View view = View.inflate(context, R.layout.view_marker, null);

TextView textView = (TextView) view.findViewById(R.id.tvQuality);

ImageView imageView = (ImageView) view.findViewById(R.id.ivQuality);

int aqi=Integer.parseInt(model.getAqi());

if (aqi>0&&aqi<=100){

imageView.setImageResource(R.drawable.ic_quality_03);

}else if(aqi>100&&aqi<=200){

imageView.setImageResource(R.drawable.ic_quality_02);

}else if (aqi>200){

imageView.setImageResource(R.drawable.ic_quality_01);

}else {

imageView.setImageResource(R.drawable.ic_quality_01);

}

textView.setText(model.getAqi());

Bitmap bitmap = convertViewToBitmap(view);

markerOptions = new MarkerOptions()

.position(latlng)

.draggable(true)

.icon(BitmapDescriptorFactory.fromBitmap(bitmap));

marker = aMap.addMarker(markerOptions);

}

}

自定义view,然后赋值,将view转化为bitmap即可:

//view 转bitmap

public static Bitmap convertViewToBitmap(View view) {

view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

view.buildDrawingCache();

Bitmap bitmap = view.getDrawingCache();

return bitmap;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义高德地图Marker,可以按照以下步骤操作: 1. 创建MarkerOptions对象,设置Marker的经纬度、图标等属性。 2. 通过AMap类的addMarker方法将Marker添加到地图上。 3. 通过AMap类的setInfoWindowAdapter方法设置Marker的信息窗口。 4. 通过AMap类的setOnMarkerClickListener方法设置Marker的点击事件。 具体实现代码如下: ``` // 创建MarkerOptions对象 MarkerOptions markerOptions = new MarkerOptions(); LatLng latLng = new LatLng(39.906901, 116.397972); markerOptions.position(latLng); markerOptions.title("Marker标题"); markerOptions.snippet("Marker描述"); markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon)); // 添加Marker到地图上 Marker marker = aMap.addMarker(markerOptions); // 设置Marker的信息窗口 aMap.setInfoWindowAdapter(new AMap.InfoWindowAdapter() { @Override public View getInfoWindow(Marker marker) { View view = getLayoutInflater().inflate(R.layout.marker_info_window, null); TextView titleTextView = view.findViewById(R.id.title); TextView snippetTextView = view.findViewById(R.id.snippet); titleTextView.setText(marker.getTitle()); snippetTextView.setText(marker.getSnippet()); return view; } @Override public View getInfoContents(Marker marker) { return null; } }); // 设置Marker的点击事件 aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.showInfoWindow(); return true; } }); ``` 其中,R.drawable.marker_icon是你自定义Marker图标。marker_info_window是你自定义Marker信息窗口布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值