본문 바로가기

프로그램/Android

네이버맵 API 사용하기(2.좌표찍기, 마커찍기)

반응형


두번째 네이버맵에 좌표찍거나 해당지역을 처음부터 띄어주는것을 하도록 하겠습니다.


네이버맵을 이용해서 맵을 띄웠을경우 setMapCenter 이라는 함수를 이용하여 해당위치를 처음부


 보이도록 할수 있습니다. setMapCenter함수에 입력하는 위도, 경도 값이 건물이여선 않됩니다.


넓은 영역이여야하죠만약 건물의 좌표를 입력하였다면 아래와같은 화면이 뜰것입니다.






대략적으로는 네이버 개발자 센터 android안에서 제가 사용한 함수들을 보시면 아주


이해하실수 있으실거 같습니다.(https://developers.naver.com/docs/map/android/)


setMarker라는 함수가 마커를 만드는 함수입니다. 그리고 테스트 하기 위해서 delay 주었구요.


네이버에서 제공하고 있는 샘플에서도 잘 나와있지만


NMapViewerResourceProvider,

NMapViewerResourceProvider

NMapPOIflagType 


클래스 파일들은 그대로 복사해온것입니다. 따로 제가 구현하지는 않았습니다.( 그런 실력이 아닌지


…) 생성되어지는 마커들을 변경하고 싶으시다면 NMapViewerResourceProvider 클래스안에 


mResourceIdsForMarkerOnMap 함수 안에서 마커이미지를 변경하시면 될거같습니다. 





이경우에는 소스도 올리고 플젝을 묶어서 올려놓도록 하겠습니다.(소스가 안올라가서 필요 Resource만 올립니다...)


1. MainActivity


public class MainActivity extends NMapActivity {

    private final String  TAG = "MainActivity";

    private ViewGroup mapLayout;

    private NMapController mMapController;
    private NMapView mMapView;

    private NMapResourceProvider nMapResourceProvider;
    private NMapOverlayManager mapOverlayManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        nMapResourceProvider = new NMapViewerResourceProvider(this);
        mapOverlayManager = new NMapOverlayManager(this, mMapView, nMapResourceProvider);
    }

    private void init(){


        mapLayout = findViewById(R.id.mapLayout);

        mMapView = new NMapView(this);
        mMapView.setClientId(getResources().getString(R.string.NAVER_API_KEY)); // 클라이언트 아이디 값 설정
        mMapView.setClickable(true);
        mMapView.setEnabled(true);
        mMapView.setFocusable(true);
        mMapView.setFocusableInTouchMode(true);
        mMapView.setScalingFactor(1.7f);        //이것을 안해주면 줌시켜서 축소 시켜도 작게보
        mMapView.requestFocus();

        mMapView.setOnMapStateChangeListener(changeListener);
        mMapView.setOnMapViewTouchEventListener(mapListener);
        mapLayout.addView(mMapView);

        mMapController = mMapView.getMapController();
        mMapController.setMapCenter(new NGeoPoint(126.978371, 37.5666091), 11);     //Default Data

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setMarker();
            }
        }, 5000);
    }

    private void setMarker(){

        int markerId = NMapPOIflagType.PIN;

        // set POI data
        NMapPOIdata poiData = new NMapPOIdata(2, nMapResourceProvider);
        poiData.beginPOIdata(2);
        poiData.addPOIitem(127.0630205, 37.5091300, "말풍선 클릭시 뿅", markerId, 0);
        poiData.addPOIitem(127.061, 37.51, "네이버맵 입니다", markerId, 0);
        poiData.endPOIdata();

        // create POI data overlay
        NMapPOIdataOverlay poiDataOverlay = mapOverlayManager.createPOIdataOverlay(poiData, null);
        poiDataOverlay.showAllPOIdata(0);
        poiDataOverlay.setOnStateChangeListener(onPOIdataStateChangeListener);  //좌표 클릭시 말풍선 리스
    }

    private NMapPOIdataOverlay.OnStateChangeListener onPOIdataStateChangeListener = new NMapPOIdataOverlay.OnStateChangeListener() {
        @Override
        public void onFocusChanged(NMapPOIdataOverlay nMapPOIdataOverlay, NMapPOIitem nMapPOIitem) {

        }

        @Override
        public void onCalloutClick(NMapPOIdataOverlay nMapPOIdataOverlay, NMapPOIitem nMapPOIitem) {
            if (nMapPOIitem != null) {
                Log.e(TAG, "onFocusChanged: " + nMapPOIitem.toString());
            } else {
                Log.e(TAG, "onFocusChanged: ");
            }
        }
    };


    private NMapView.OnMapStateChangeListener changeListener = new NMapView.OnMapStateChangeListener() {
        @Override
        public void onMapInitHandler(NMapView nMapView, NMapError nMapError) {
            Log.e(TAG, "OnMapStateChangeListener onMapInitHandler : ");
        }

        @Override
        public void onMapCenterChange(NMapView nMapView, NGeoPoint nGeoPoint) {
            Log.e(TAG, "OnMapStateChangeListener onMapCenterChange : " + nGeoPoint.getLatitude() + " ㅡ  " + nGeoPoint.getLongitude());
        }

        @Override
        public void onMapCenterChangeFine(NMapView nMapView) {
            Log.e(TAG, "OnMapStateChangeListener onMapCenterChangeFine : ");
        }

        @Override
        public void onZoomLevelChange(NMapView nMapView, int i) {
            Log.e(TAG, "OnMapStateChangeListener onZoomLevelChange : " + i);
        }

        @Override
        public void onAnimationStateChange(NMapView nMapView, int i, int i1) {
            Log.e(TAG, "OnMapStateChangeListener onAnimationStateChange : ");
        }
    };

    private NMapView.OnMapViewTouchEventListener mapListener = new NMapView.OnMapViewTouchEventListener() {
        @Override
        public void onLongPress(NMapView nMapView, MotionEvent motionEvent) {
            Log.e(TAG, "OnMapViewTouchEventListener onLongPress : ");
        }

        @Override
        public void onLongPressCanceled(NMapView nMapView) {
            Log.e(TAG, "OnMapViewTouchEventListener onLongPressCanceled : ");
        }

        @Override
        public void onTouchDown(NMapView nMapView, MotionEvent motionEvent) {
            Log.e(TAG, "OnMapViewTouchEventListener onTouchDown : ");
        }

        @Override
        public void onTouchUp(NMapView nMapView, MotionEvent motionEvent) {
            Log.e(TAG, "OnMapViewTouchEventListener onTouchUp : ");
        }

        @Override
        public void onScroll(NMapView nMapView, MotionEvent motionEvent, MotionEvent motionEvent1) {
            Log.e(TAG, "OnMapViewTouchEventListener onScroll : ");
        }

        @Override
        public void onSingleTapUp(NMapView nMapView, MotionEvent motionEvent) {
            Log.e(TAG, "OnMapViewTouchEventListener onSingleTapUp : ");
        }
    };
}



2. NMapCalloutCustomOldOverlay



public class NMapCalloutCustomOldOverlay extends NMapCalloutOverlay {

    private static final int CALLOUT_TEXT_COLOR = 0xFFFFFFFF;
    private static final float CALLOUT_TEXT_SIZE = 16.0F;
    private static final Typeface CALLOUT_TEXT_TYPEFACE = null;//Typeface.DEFAULT_BOLD;

    private static final float CALLOUT_RIGHT_BUTTON_WIDTH = 50.67F;
    private static final float CALLOUT_RIGHT_BUTTON_HEIGHT = 34.67F;

    private static final float CALLOUT_MARGIN_X = 9.33F;
    private static final float CALLOUT_PADDING_X = 9.33F;
    private static final float CALLOUT_PADDING_OFFSET = 0.45F;
    private static final float CALLOUT_PADDING_Y = 17.33F;
    private static final float CALLOUT_MIMIMUM_WIDTH = 63.33F;
    private static final float CALLOUT_TOTAL_HEIGHT = 64.0F;
    private static final float CALLOUT_BACKGROUND_HEIGHT = CALLOUT_PADDING_Y + CALLOUT_TEXT_SIZE + CALLOUT_PADDING_Y;
    private static final float CALLOUT_ITEM_GAP_Y = 0.0F;
    private static final float CALLOUT_TAIL_GAP_X = 6.67F;
    private static final float CALLOUT_TITLE_OFFSET_Y = -2.0F;

    private final TextPaint mTextPaint = new TextPaint();
    private float mOffsetX, mOffsetY;

    private final float mMarginX;
    private final float mPaddingX, mPaddingY, mPaddingOffset;
    private final float mMinimumWidth;
    private final float mTotalHeight;
    private final float mBackgroundHeight;
    private final float mItemGapY;
    private final float mTailGapX;
    private final float mTitleOffsetY;

    private final Drawable mBackgroundDrawable;
    protected final Rect mTemp2Rect = new Rect();
    private final Rect mRightButtonRect;
    private final String mRightButtonText;
    private final int mCalloutRightButtonWidth;
    private final int mCalloutRightButtonHeight;
    private Drawable[] mDrawableRightButton;
    private final int mCalloutButtonCount = 1;

    private String mTitleTruncated;
    private int mWidthTitleTruncated;

    private final String mTailText;
    private float mTailTextWidth;

    /**
     * Resource provider should implement this interface
     */
    public static interface ResourceProvider {

        public Drawable getCalloutBackground(NMapOverlayItem item);

        public String getCalloutRightButtonText(NMapOverlayItem item);

        public Drawable[] getCalloutRightButton(NMapOverlayItem item);

        public Drawable[] getCalloutRightAccessory(NMapOverlayItem item);
    }

    public NMapCalloutCustomOldOverlay(NMapOverlay itemOverlay, NMapOverlayItem item, Rect itemBounds,
                                       NMapCalloutCustomOldOverlay.ResourceProvider resourceProvider) {

        super(itemOverlay, item, itemBounds);

        mTextPaint.setAntiAlias(true);
        // set font style
        mTextPaint.setColor(CALLOUT_TEXT_COLOR);
        // set font size
        mTextPaint.setTextSize(CALLOUT_TEXT_SIZE * NMapResourceProvider.getScaleFactor());
        // set font type
        if (CALLOUT_TEXT_TYPEFACE != null) {
            mTextPaint.setTypeface(CALLOUT_TEXT_TYPEFACE);
        }

        mMarginX = NMapResourceProvider.toPixelFromDIP(CALLOUT_MARGIN_X);
        mPaddingX = NMapResourceProvider.toPixelFromDIP(CALLOUT_PADDING_X);
        mPaddingOffset = NMapResourceProvider.toPixelFromDIP(CALLOUT_PADDING_OFFSET);
        mPaddingY = NMapResourceProvider.toPixelFromDIP(CALLOUT_PADDING_Y);
        mMinimumWidth = NMapResourceProvider.toPixelFromDIP(CALLOUT_MIMIMUM_WIDTH);
        mTotalHeight = NMapResourceProvider.toPixelFromDIP(CALLOUT_TOTAL_HEIGHT);
        mBackgroundHeight = NMapResourceProvider.toPixelFromDIP(CALLOUT_BACKGROUND_HEIGHT);
        mItemGapY = NMapResourceProvider.toPixelFromDIP(CALLOUT_ITEM_GAP_Y);

        mTailGapX = NMapResourceProvider.toPixelFromDIP(CALLOUT_TAIL_GAP_X);
        mTailText = item.getTailText();

        mTitleOffsetY = NMapResourceProvider.toPixelFromDIP(CALLOUT_TITLE_OFFSET_Y);

        if (resourceProvider == null) {
            throw new IllegalArgumentException(
                    "NMapCalloutCustomOverlay.ResourceProvider should be provided on creation of NMapCalloutCustomOverlay.");
        }

        mBackgroundDrawable = resourceProvider.getCalloutBackground(item);

        boolean hasRightAccessory = false;
        mDrawableRightButton = resourceProvider.getCalloutRightAccessory(item);
        if (mDrawableRightButton != null && mDrawableRightButton.length > 0) {
            hasRightAccessory = true;

            mRightButtonText = null;
        } else {
            mDrawableRightButton = resourceProvider.getCalloutRightButton(item);
            mRightButtonText = resourceProvider.getCalloutRightButtonText(item);
        }

        if (mDrawableRightButton != null) {
            if (hasRightAccessory) {
                mCalloutRightButtonWidth = mDrawableRightButton[0].getIntrinsicWidth();
                mCalloutRightButtonHeight = mDrawableRightButton[0].getIntrinsicHeight();
            } else {
                mCalloutRightButtonWidth = NMapResourceProvider.toPixelFromDIP(CALLOUT_RIGHT_BUTTON_WIDTH);
                mCalloutRightButtonHeight = NMapResourceProvider.toPixelFromDIP(CALLOUT_RIGHT_BUTTON_HEIGHT);
            }

            mRightButtonRect = new Rect();

            super.setItemCount(mCalloutButtonCount);
        } else {
            mCalloutRightButtonWidth = 0;
            mCalloutRightButtonHeight = 0;
            mRightButtonRect = null;
        }

        mTitleTruncated = null;
        mWidthTitleTruncated = 0;
    }

    @Override
    protected boolean hitTest(int hitX, int hitY) {

        // hit test for right button only ?
        //    	if (mRightButtonRect != null) {
        //    		return  mRightButtonRect.contains(hitX, hitY);
        //    	}

        return super.hitTest(hitX, hitY);
    }

    @Override
    protected boolean isTitleTruncated() {
        return (mTitleTruncated != mOverlayItem.getTitle());
    }

    @Override
    protected int getMarginX() {
        return (int)(mMarginX);
    }

    @Override
    protected Rect getBounds(NMapView mapView) {

        adjustTextBounds(mapView);

        mTempRect.set((int)mTempRectF.left, (int)mTempRectF.top, (int)mTempRectF.right, (int)mTempRectF.bottom);
        mTempRect.union(mTempPoint.x, mTempPoint.y);

        return mTempRect;
    }

    @Override
    protected PointF getSclaingPivot() {
        PointF pivot = new PointF();

        pivot.x = mTempRectF.centerX();
        pivot.y = mTempRectF.top + mTotalHeight;

        return pivot;
    }

    @Override
    protected void drawCallout(Canvas canvas, NMapView mapView, boolean shadow, long when) {

        adjustTextBounds(mapView);

        stepAnimations(canvas, mapView, when);

        drawBackground(canvas);

        float left, top;

        // draw title
        mOffsetX = mTempPoint.x - mTempRect.width() / 2;
        mOffsetX -= mPaddingOffset;
        mOffsetY = mTempRectF.top + mPaddingY + mTextPaint.getTextSize() + mTitleOffsetY;
        canvas.drawText(mTitleTruncated, mOffsetX, mOffsetY, mTextPaint);

        // draw right button
        if (mDrawableRightButton != null) {
            left = mTempRectF.right - mPaddingX - mCalloutRightButtonWidth;
            top = mTempRectF.top + (mBackgroundHeight - mCalloutRightButtonHeight) / 2;

            // Use background drawables depends on current state
            mRightButtonRect.left = (int)(left + 0.5F);
            mRightButtonRect.top = (int)(top + 0.5F);
            mRightButtonRect.right = (int)(left + mCalloutRightButtonWidth + 0.5F);
            mRightButtonRect.bottom = (int)(top + mCalloutRightButtonHeight + 0.5F);

            int itemState = super.getItemState(0);
            Drawable drawable = getDrawable(0, itemState);
            if (drawable != null) {
                drawable.setBounds(mRightButtonRect);
                drawable.draw(canvas);
            }

            if (mRightButtonText != null) {
                mTextPaint.getTextBounds(mRightButtonText, 0, mRightButtonText.length(), mTempRect);

                left = mRightButtonRect.left + (mCalloutRightButtonWidth - mTempRect.width()) / 2;
                top = mRightButtonRect.top + (mCalloutRightButtonHeight - mTempRect.height()) / 2 + mTempRect.height()
                        + mTitleOffsetY;
                canvas.drawText(mRightButtonText, left, top, mTextPaint);
            }
        }

        // draw tail text
        if (mTailText != null) {
            if (mRightButtonRect != null) {
                left = mRightButtonRect.left;
            } else {
                left = mTempRectF.right;
            }
            left -= mPaddingX + mTailTextWidth;
            top = mOffsetY;

            canvas.drawText(mTailText, left, top, mTextPaint);
        }
    }

	/* Internal Functions */

    private void drawBackground(Canvas canvas) {

        mTemp2Rect.left = (int)(mTempRectF.left + 0.5F);
        mTemp2Rect.top = (int)(mTempRectF.top + 0.5F);
        mTemp2Rect.right = (int)(mTempRectF.right + 0.5F);
        mTemp2Rect.bottom = (int)(mTempRectF.top + mTotalHeight + 0.5F);

        mBackgroundDrawable.setBounds(mTemp2Rect);
        mBackgroundDrawable.draw(canvas);
    }

    private void adjustTextBounds(NMapView mapView) {

        //  First determine the screen coordinates of the selected MapLocation
        mapView.getMapProjection().toPixels(mOverlayItem.getPointInUtmk(), mTempPoint);

        int mapViewWidth = mapView.getMapController().getViewFrameVisibleWidth();
        if (mTitleTruncated == null || mWidthTitleTruncated != mapViewWidth) {
            mWidthTitleTruncated = mapViewWidth;
            float maxWidth = mWidthTitleTruncated - 2 * mMarginX - 2 * mPaddingX;

            if (mDrawableRightButton != null) {
                maxWidth -= mPaddingX + mCalloutRightButtonWidth;
            }

            if (mTailText != null) {
                mTextPaint.getTextBounds(mTailText, 0, mTailText.length(), mTempRect);
                mTailTextWidth = mTempRect.width();

                maxWidth -= mTailGapX + mTailTextWidth;
            }

            final String title = TextUtils.ellipsize(mOverlayItem.getTitle(), mTextPaint, maxWidth,
                    TextUtils.TruncateAt.END).toString();

            mTitleTruncated = title;

        }

        mTextPaint.getTextBounds(mTitleTruncated, 0, mTitleTruncated.length(), mTempRect);

        if (mDrawableRightButton != null) {
            mTempRect.right += mPaddingX + mCalloutRightButtonWidth;
        }

        if (mTailText != null) {
            mTempRect.right += mTailGapX + mTailTextWidth;
        }

        //  Setup the callout with the right size & location
        mTempRectF.set(mTempRect);
        final float dy = (mBackgroundHeight - mTempRect.height()) / 2;
        mTempRectF.inset(-mPaddingX, -dy);
        //mTempRectF.inset(-mPaddingX, -mPaddingY);

        // set minimum size
        if (mTempRectF.width() < mMinimumWidth) {
            final float dx = (mMinimumWidth - mTempRectF.width()) / 2;
            mTempRectF.inset(-dx, 0);
        }

        // set position
        float left = mTempPoint.x - (int)(mTempRectF.width() * mOverlayItem.getAnchorXRatio());
        float top = mTempPoint.y - (int)(mItemBounds.height() * mOverlayItem.getAnchorYRatio()) - mItemGapY
                - mTotalHeight;
        mTempRectF.set(left, top, left + mTempRectF.width(), top + mTempRectF.height());

    }

    @Override
    protected Drawable getDrawable(int rank, int itemState) {
        if (mDrawableRightButton != null && mDrawableRightButton.length >= 3) {
            int idxDrawable = 0;
            if (NMapOverlayItem.isPressedState(itemState)) {
                idxDrawable = 1;
            } else if (NMapOverlayItem.isSelectedState(itemState)) {
                idxDrawable = 2;
            } else if (NMapOverlayItem.isFocusedState(itemState)) {
                idxDrawable = 2;
            }
            Drawable drawable = mDrawableRightButton[idxDrawable];
            return drawable;
        }

        return null;
    }
}



3. NMapPOIflagType



public class NMapPOIflagType {

    public static final int UNKNOWN = 0x0000;

    // Single POI icons
    private static final int SINGLE_POI_BASE = 0x0100;

    // Spot, Pin icons
    public static final int SPOT = SINGLE_POI_BASE + 1;
    public static final int PIN = SPOT + 1;

    // Direction POI icons: From, To
    private static final int DIRECTION_POI_BASE = 0x0200;
    public static final int FROM = DIRECTION_POI_BASE + 1;
    public static final int TO = FROM + 1;

    // end of single marker icon
    public static final int SINGLE_MARKER_END = 0x04FF;

    // Direction Number icons
    private static final int MAX_NUMBER_COUNT = 1000;
    public static final int NUMBER_BASE = 0x1000; // set NUMBER_BASE + 1 for '1' number
    public static final int NUMBER_END = NUMBER_BASE + MAX_NUMBER_COUNT;

    // Custom POI icons
    private static final int MAX_CUSTOM_COUNT = 1000;
    public static final int CUSTOM_BASE = NUMBER_END;
    public static final int CUSTOM_END = CUSTOM_BASE + MAX_CUSTOM_COUNT;

    // Clickable callout에 보여지는 화살표
    public static final int CLICKABLE_ARROW = CUSTOM_END + 1;

    public static boolean isBoundsCentered(int markerId) {
        boolean boundsCentered = false;

        switch (markerId) {
            default:
                if (markerId >= NMapPOIflagType.NUMBER_BASE && markerId < NMapPOIflagType.NUMBER_END) {
                    boundsCentered = true;
                }
                break;
        }

        return boundsCentered;
    }

    public static int getMarkerId(int poiFlagType, int iconIndex) {
        int markerId = poiFlagType + iconIndex;

        return markerId;
    }

    public static int getPOIflagType(int markerId) {
        int poiFlagType = UNKNOWN;

        // Alphabet POI icons
        if (markerId >= NUMBER_BASE && markerId < NUMBER_END) { // Direction Number icons
            poiFlagType = NUMBER_BASE;
        } else if (markerId >= CUSTOM_BASE && markerId < CUSTOM_END) { // Custom POI icons
            poiFlagType = CUSTOM_BASE;
        } else if (markerId > SINGLE_POI_BASE) {
            poiFlagType = markerId;
        }

        return poiFlagType;
    }

    public static int getPOIflagIconIndex(int markerId) {
        int iconIndex = 0;

        if (markerId >= NUMBER_BASE && markerId < NUMBER_END) { // Direction Number icons
            iconIndex = markerId - (NUMBER_BASE + 1);
        } else if (markerId >= CUSTOM_BASE && markerId < CUSTOM_END) { // Custom POI icons
            iconIndex = markerId - (CUSTOM_BASE + 1);
        } else if (markerId > SINGLE_POI_BASE) {
            iconIndex = 0;
        }

        return iconIndex;
    }
}



4. NMapViewerResourceProvider



public class NMapViewerResourceProvider extends NMapResourceProvider implements
        NMapCalloutCustomOldOverlay.ResourceProvider {
    private static final String LOG_TAG = "NMapViewerResourceProvider";
    private static final boolean DEBUG = false;

    private static final Bitmap.Config BITMAP_CONFIG_DEFAULT = Bitmap.Config.ARGB_8888;

    private static final int POI_FONT_COLOR_NUMBER = 0xFF909090;
    private static final float POI_FONT_SIZE_NUMBER = 10.0F;

    private static final int POI_FONT_COLOR_ALPHABET = 0xFFFFFFFF;
    private static final float POI_FONT_OFFSET_ALPHABET = 6.0F;
    private static final Typeface POI_FONT_TYPEFACE = null;//Typeface.DEFAULT_BOLD;

    private static final int CALLOUT_TEXT_COLOR_NORMAL = 0xFFFFFFFF;
    private static final int CALLOUT_TEXT_COLOR_PRESSED = 0xFF9CA1AA;
    private static final int CALLOUT_TEXT_COLOR_SELECTED = 0xFFFFFFFF;
    private static final int CALLOUT_TEXT_COLOR_FOCUSED = 0xFFFFFFFF;

    private final Rect mTempRect = new Rect();
    private final Paint mTextPaint = new Paint();

    public NMapViewerResourceProvider(Context context) {
        super(context);

        mTextPaint.setAntiAlias(true);
    }

    /**
     * Get drawable for markerId at focused state
     *
     * @param markerId unique id for POI or Number icons.
     * @param focused true for focused state, false otherwise.
     * @return
     */
    public Drawable getDrawable(int markerId, boolean focused, NMapOverlayItem item) {
        Drawable marker = null;

        int resourceId = findResourceIdForMarker(markerId, focused);
        if (resourceId > 0) {
            marker = mContext.getResources().getDrawable(resourceId);
        } else {
            resourceId = 4 * markerId;
            if (focused) {
                resourceId += 1;
            }

            marker = getDrawableForMarker(markerId, focused, item);
        }

        // set bounds
        if (marker != null) {
            setBounds(marker, markerId, item);
        }

        return marker;
    }

    public Bitmap getBitmap(int markerId, boolean focused, NMapOverlayItem item) {
        Bitmap bitmap = null;

        Drawable marker = getDrawable(markerId, focused, item);
        if (marker != null) {
            bitmap = getBitmap(marker);
        }

        return bitmap;
    }

    public Bitmap getBitmap(Drawable marker) {
        Bitmap bitmap = null;

        if (marker != null) {
            int width = marker.getIntrinsicWidth();
            int height = marker.getIntrinsicHeight();
            bitmap = Bitmap.createBitmap(width, height, BITMAP_CONFIG_DEFAULT);

            marker.setBounds(0, 0, width, height);

            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(0x00000000);

            marker.draw(canvas);
        }

        return bitmap;
    }

    public Bitmap getBitmap(int resourceId) {
        Bitmap bitmap = null;

        Drawable marker = null;
        if (resourceId > 0) {
            marker = mContext.getResources().getDrawable(resourceId);
        }

        if (marker != null) {
            bitmap = getBitmap(marker);
        }

        return bitmap;
    }

    public Bitmap getBitmapWithNumber(int resourceId, String strNumber, float offsetY, int fontColor, float fontSize) {
        Bitmap bitmap = null;

        Drawable marker = getDrawableWithNumber(resourceId, strNumber, offsetY, fontColor, fontSize);

        if (marker != null) {
            bitmap = getBitmap(marker);
        }

        return bitmap;
    }

    @Override
    public Drawable getDrawableForInfoLayer(NMapOverlayItem item) {
        return null;
    }

    /**
     * Class to find resource Ids on map view
     */
    private class ResourceIdsOnMap {

        int markerId;
        int resourceId;
        int resourceIdFocused;

        ResourceIdsOnMap(int markerId, int resourceId, int resourceIdFocused) {
            this.markerId = markerId;
            this.resourceId = resourceId;
            this.resourceIdFocused = resourceIdFocused;
        }
    }

    // Resource Ids for single icons
    private final ResourceIdsOnMap mResourceIdsForMarkerOnMap[] = {
            // Spot, Pin icons
            new ResourceIdsOnMap(NMapPOIflagType.PIN, R.drawable.ic_pin_01, R.drawable.ic_pin_02),
            new ResourceIdsOnMap(NMapPOIflagType.SPOT, R.drawable.ic_pin_01, R.drawable.ic_pin_02),

            // Direction POI icons: From, To
            new ResourceIdsOnMap(NMapPOIflagType.FROM, R.drawable.ic_map_start, R.drawable.ic_map_start_over),
            new ResourceIdsOnMap(NMapPOIflagType.TO, R.drawable.ic_map_arrive, R.drawable.ic_map_arrive_over),
    };

    /**
     * Find resource id corresponding to the markerId.
     *
     * @param markerId marker id for a NMapPOIitem.
     * @param focused flag to indicated focused or normal state of this marker.
     *
     * @return resource id for the given markerId.
     *
     * @see NMapPOIflagType
     */
    @Override
    protected int findResourceIdForMarker(int markerId, boolean focused) {
        int resourceId = 0;

        if (DEBUG) {
            Log.e(LOG_TAG, "getResourceIdForMarker: markerId=" + markerId + ", focused=" + focused);
        }

        if (markerId < NMapPOIflagType.SINGLE_MARKER_END) {
            resourceId = getResourceIdOnMapView(markerId, focused, mResourceIdsForMarkerOnMap);
            if (resourceId > 0) {
                return resourceId;
            }
        }

        if (markerId >= NMapPOIflagType.NUMBER_BASE && markerId < NMapPOIflagType.NUMBER_END) { // Direction Number icons

        } else if (markerId >= NMapPOIflagType.CUSTOM_BASE && markerId < NMapPOIflagType.CUSTOM_END) { // Custom POI icons

        }

        return resourceId;
    }

    /**
     * 	Set bounds for this marker depending on its shape.
     *
     */
    @Override
    protected void setBounds(Drawable marker, int markerId, NMapOverlayItem item) {

        // check shape of the marker to set bounds correctly.
        if (NMapPOIflagType.isBoundsCentered(markerId)) {
            if (marker.getBounds().isEmpty()) {
                NMapOverlayItem.boundCenter(marker);
            }

            if (null != item) {
                item.setAnchorRatio(0.5f, 0.5f);
            }

        } else {
            if (marker.getBounds().isEmpty()) {
                NMapOverlayItem.boundCenterBottom(marker);
            }

            if (null != item) {
                item.setAnchorRatio(0.5f, 1.0f);
            }
        }
    }

    @Override
    public Drawable[] getLocationDot() {
        Drawable[] drawable = new Drawable[2];

        drawable[0] = mContext.getResources().getDrawable(R.drawable.pubtrans_ic_mylocation_off);
        drawable[1] = mContext.getResources().getDrawable(R.drawable.pubtrans_ic_mylocation_on);

        for (int i = 0; i < drawable.length; i++) {
            int w = drawable[i].getIntrinsicWidth() / 2;
            int h = drawable[i].getIntrinsicHeight() / 2;

            drawable[i].setBounds(-w, -h, w, h);
        }

        return drawable;
    }

    @Override
    public Drawable getDirectionArrow() {

        Drawable drawable = mContext.getResources().getDrawable(R.drawable.ic_angle);

        if (drawable != null) {
            int w = drawable.getIntrinsicWidth() / 2;
            int h = drawable.getIntrinsicHeight() / 2;

            drawable.setBounds(-w, -h, w, h);
        }

        return drawable;
    }

    public Drawable getDrawableWithNumber(int resourceId, String strNumber, float offsetY, int fontColor, float fontSize) {

        Bitmap textBitmap = getBitmapWithText(resourceId, strNumber, fontColor, fontSize, offsetY);

        //Log.i(LOG_TAG, "getDrawableWithNumber: width=" + textBitmap.getWidth() + ", height=" + textBitmap.getHeight() + ", density=" + textBitmap.getDensity());

        // set bounds
        Drawable marker = new BitmapDrawable(mContext.getResources(), textBitmap);
        if (marker != null) {
            NMapOverlayItem.boundCenter(marker);
        }

        //Log.i(LOG_TAG, "getDrawableWithNumber: width=" + marker.getIntrinsicWidth() + ", height=" + marker.getIntrinsicHeight());

        return marker;
    }

    public Drawable getDrawableWithAlphabet(int resourceId, String strAlphabet, int fontColor, float fontSize) {

        Bitmap textBitmap = getBitmapWithText(resourceId, strAlphabet, fontColor, fontSize, POI_FONT_OFFSET_ALPHABET);

        // set bounds
        Drawable marker = new BitmapDrawable(mContext.getResources(), textBitmap);
        if (marker != null) {
            NMapOverlayItem.boundCenterBottom(marker);
        }

        return marker;
    }

    @Override
    protected Drawable getDrawableForMarker(int markerId, boolean focused, NMapOverlayItem item) {
        Drawable drawable = null;

        if (markerId >= NMapPOIflagType.NUMBER_BASE && markerId < NMapPOIflagType.NUMBER_END) { // Direction Number icons
            int resourceId = (focused) ? R.drawable.ic_map_no_02 : R.drawable.ic_map_no_01;
            int fontColor = (focused) ? POI_FONT_COLOR_ALPHABET : POI_FONT_COLOR_NUMBER;

            String strNumber = String.valueOf(markerId - NMapPOIflagType.NUMBER_BASE);

            drawable = getDrawableWithNumber(resourceId, strNumber, 0.0F, fontColor, POI_FONT_SIZE_NUMBER);
        } else if (markerId >= NMapPOIflagType.CUSTOM_BASE && markerId < NMapPOIflagType.CUSTOM_END) { // Custom POI icons

        }

        return drawable;
    }

    private Bitmap getBitmapWithText(int resourceId, String strNumber, int fontColor, float fontSize, float offsetY) {
        Bitmap bitmapBackground = BitmapFactory.decodeResource(mContext.getResources(), resourceId);

        int width = bitmapBackground.getWidth();
        int height = bitmapBackground.getHeight();
        //Log.i(LOG_TAG, "getBitmapWithText: width=" + width + ", height=" + height + ", density=" + bitmapBackground.getDensity());

        Bitmap textBitmap = Bitmap.createBitmap(width, height, BITMAP_CONFIG_DEFAULT);

        Canvas canvas = new Canvas(textBitmap);

        canvas.drawBitmap(bitmapBackground, 0, 0, null);

        // set font style
        mTextPaint.setColor(fontColor);
        // set font size
        mTextPaint.setTextSize(fontSize * mScaleFactor);
        // set font type
        if (POI_FONT_TYPEFACE != null) {
            mTextPaint.setTypeface(POI_FONT_TYPEFACE);
        }

        // get text offset
        mTextPaint.getTextBounds(strNumber, 0, strNumber.length(), mTempRect);
        float offsetX = (width - mTempRect.width()) / 2 - mTempRect.left;
        if (offsetY == 0.0F) {
            offsetY = (height - mTempRect.height()) / 2 + mTempRect.height();
        } else {
            offsetY = offsetY * mScaleFactor + mTempRect.height();
        }

        //Log.i(LOG_TAG, "getBitmapWithText: number=" + number + ", focused=" + focused);
        //Log.i(LOG_TAG, "getBitmapWithText: offsetX=" + offsetX + ", offsetY=" + offsetY + ", boundsWidth=" + mTempRect.width() + ", boundsHeight=" + mTempRect.height());

        // draw text
        canvas.drawText(strNumber, offsetX, offsetY, mTextPaint);

        return textBitmap;
    }

    @Override
    public Drawable getCalloutBackground(NMapOverlayItem item) {

        if (item instanceof NMapPOIitem) {
            NMapPOIitem poiItem = (NMapPOIitem)item;

            if (poiItem.showRightButton()) {
                Drawable drawable = mContext.getResources().getDrawable(R.drawable.bg_speech);
                return drawable;
            }
        }

        Drawable drawable = mContext.getResources().getDrawable(R.drawable.pin_ballon_bg);

        return drawable;
    }

    @Override
    public String getCalloutRightButtonText(NMapOverlayItem item) {
        if (item instanceof NMapPOIitem) {
            NMapPOIitem poiItem = (NMapPOIitem)item;

            if (poiItem.showRightButton()) {
                return mContext.getResources().getString(R.string.str_done);
            }
        }

        return null;
    }

    @Override
    public Drawable[] getCalloutRightButton(NMapOverlayItem item) {
        if (item instanceof NMapPOIitem) {
            NMapPOIitem poiItem = (NMapPOIitem)item;

            if (poiItem.showRightButton()) {
                Drawable[] drawable = new Drawable[3];

                drawable[0] = mContext.getResources().getDrawable(R.drawable.btn_green_normal);
                drawable[1] = mContext.getResources().getDrawable(R.drawable.btn_green_pressed);
                drawable[2] = mContext.getResources().getDrawable(R.drawable.btn_green_highlight);

                return drawable;
            }
        }

        return null;
    }

    @Override
    public Drawable[] getCalloutRightAccessory(NMapOverlayItem item) {
        if (item instanceof NMapPOIitem) {
            NMapPOIitem poiItem = (NMapPOIitem)item;

            if (poiItem.hasRightAccessory() && (poiItem.getRightAccessoryId() > 0)) {
                Drawable[] drawable = new Drawable[3];

                switch (poiItem.getRightAccessoryId()) {
                    case NMapPOIflagType.CLICKABLE_ARROW:
                        drawable[0] = mContext.getResources().getDrawable(R.drawable.pin_ballon_arrow);
                        drawable[1] = mContext.getResources().getDrawable(R.drawable.pin_ballon_on_arrow);
                        drawable[2] = mContext.getResources().getDrawable(R.drawable.pin_ballon_on_arrow);
                        break;
                }

                return drawable;
            }
        }

        return null;
    }

    /**
     * 말풍선의 텍스트 색상을 customize한다.
     *
     * @param item
     * @return
     * @see com.nhn.android.mapviewer.overlay.NMapCalloutCustomOverlay.ResourceProvider#getCalloutTextColors(com.nhn.android.maps.NMapOverlayItem)
     */
    @Override
    public int[] getCalloutTextColors(NMapOverlayItem item) {
        int[] colors = new int[4];
        colors[0] = CALLOUT_TEXT_COLOR_NORMAL;
        colors[1] = CALLOUT_TEXT_COLOR_PRESSED;
        colors[2] = CALLOUT_TEXT_COLOR_SELECTED;
        colors[3] = CALLOUT_TEXT_COLOR_FOCUSED;
        return colors;
    }

    @Override
    public int getParentLayoutIdForOverlappedListView() {
        // not supported
        return 0;
    }

    @Override
    public int getOverlappedListViewId() {
        // not supported
        return 0;
    }

    @Override
    public int getLayoutIdForOverlappedListView() {
        // not supported
        return 0;
    }

    @Override
    public int getListItemLayoutIdForOverlappedListView() {
        // not supported
        return 0;
    }

    @Override
    public int getListItemTextViewId() {
        // not supported
        return 0;
    }

    @Override
    public int getListItemTailTextViewId() {
        // not supported
        return 0;
    }

    @Override
    public int getListItemImageViewId() {
        // not supported
        return 0;
    }

    @Override
    public int getListItemDividerId() {
        // not supported
        return 0;
    }

    @Override
    public void setOverlappedListViewLayout(ListView listView, int itemCount, int width, int height) {
        // not supported
    }

    @Override
    public void setOverlappedItemResource(NMapPOIitem poiItem, ImageView imageView) {
        // not supported
    }

    private int getResourceIdOnMapView(int markerId, boolean focused, ResourceIdsOnMap resourceIdsArray[]) {
        int resourceId = 0;

        for (ResourceIdsOnMap resourceIds : resourceIdsArray) {
            if (resourceIds.markerId == markerId) {
                resourceId = (focused) ? resourceIds.resourceIdFocused : resourceIds.resourceId;
                break;
            }
        }

        return resourceId;
    }
}



아카이브.zip

반응형