Android 谷歌地图在自定义 Marker 上添加自定义 View

前段时间项目中嵌入了 android 高德地图,但是后来发现国外的客户反应高德地图加载很慢(高德地图显示国外地图的方法:在初始化时调用 MapsInitializer.loadWorldGridMap(boolean) true 表示打开),后来在售后的不断反馈下决定尝试国内加载高德地图,国外加载谷歌地图,这样应该就能解决高德地图在国外加载超慢而且还不能显示卫星地图的情况了。

 

工作开始了,高德地图转 Google 地图进展还是挺顺利的,这两种地图 api 极其类似,方法名都高度保持一致,不过还是有不同的地方,其中有一个方法是将自定义 view 添加到 Marker 显示到地图上,高德地图有提供方法:

BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromView(view);

但是Google地图没有这个方法,于是我就查看 fromView() 的源码,最终照葫芦画瓢成功的实现了一个 Google 地图版的   fromView(),具体实现如下:

/** GoogleMap:根据传入的 view,创建 BitmapDescriptor 对象 */
public BitmapDescriptor fromView(Context context, View view) {
    FrameLayout frameLayout = new FrameLayout(context);
    frameLayout.addView(view);
    frameLayout.setDrawingCacheEnabled(true);
    Bitmap bitmap = getBitmapFromView(frameLayout);
    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
    bitmap.recycle();
    return bitmapDescriptor;
}

/** Convert a view to bitmap */
public Bitmap getBitmapFromView(View view) {
    try {
        banTextViewHorizontallyScrolling(view);
        view.destroyDrawingCache();
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        Bitmap bitmap = view.getDrawingCache();
        return bitmap != null ? bitmap.copy(Bitmap.Config.ARGB_8888, false) : null;
    } catch (Throwable ex) {
        Log.d(TAG, "getBitmapFromView: ");
        return null;
    }
}

/** 禁止 TextView 水平滚动 */
private void banTextViewHorizontallyScrolling(View view) {
    if (view instanceof ViewGroup) {
        for (int index = 0; index < ((ViewGroup) view).getChildCount(); ++index) {
            banTextViewHorizontallyScrolling(((ViewGroup) view).getChildAt(index));
        }
    } else if (view instanceof TextView) {
        ((TextView) view).setHorizontallyScrolling(false);
    }
}

使用方法如下:

View view = LayoutInflater.from(this).inflate(R.layout.googlemap_marker_layout, null);

BitmapDescriptor bitmapDescriptor = fromView(getActivity(), view);
MarkerOptions option = new MarkerOptions()
        .position(lastLatLng)
        .icon(bitmapDescriptor)
        .zIndex(4)
        .draggable(false);

Marker marker = mGoogleMap.addMarker(option);

这样就完美的将自定义 View 添加成 Marker 并显示在地图上了。

 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自定义高德地图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信息窗口布局。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值