Android集成高德地圖如何自定義marker

高德地圖自定義Marker
高德地圖默認的marker樣式是這種

通常的修改樣式是經過icon接口來調整ui

MarkerOptions markerOptions = new MarkerOptions()
                    .position(latlng)
                    .draggable(true)
                    .icon(BitmapDescriptorFactory.fromBitmap(bitmap));

當遇到圖片加文字的marker該如何自定義呢?如這樣

步驟:.net

/**
     * 往地圖上添加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便可:code

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

原文:https://blog.csdn.net/qq_16131393/article/details/78252857blog

相關文章
相關標籤/搜索