ЗДЕСЬ SDK - Карта Масштабная линейка - PullRequest
0 голосов
/ 23 февраля 2019

Я использую Android Premium SDK API-карты HERE, как описано в https://developer.here.com/documentation/android-premium/dev_guide/topics/maps.html

Чего я хочу добиться, так это нарисовать масштабную линейку карты в правом нижнем углу.Кто-нибудь может привести пример, как этого добиться?

Если вам нужна дополнительная информация, не стесняйтесь спрашивать в комментарии.Спасибо Томас

1 Ответ

0 голосов
/ 28 февраля 2019

Вы можете сделать следующее:

  1. Создать представление масштаба
  2. Вставить его в свой MapFragment
  3. Прикрепить его к карте

Создание масштабного представления (пожалуйста, измените, где необходимо для вашего приложения)

package com.here.testapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapState;

import static java.lang.Math.floor;

public class MapScaleView extends View implements Map.OnTransformListener{
    private static final float VIEW_WIDTH_INCH = 1.f,
                               VIEW_HEIGHT_INCH = .2f;

    private static final int RULER_STROKE_WIDTH_PX = 10;
    private static final int RULER_HEIGHT_PX = 30;
    private final Paint painter = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Rect rulerRects[] = {new Rect(), new Rect(), new Rect()};
    private String m_text = "NaN";
    private Map m_map;

    public MapScaleView(Context context) {
        super(context);
    }

    public MapScaleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MapScaleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setMap(Map map) {
        if (m_map != null) {
            m_map.removeTransformListener(this);
        }
        m_map = map;
        if (m_map != null) {
            m_map.addTransformListener(this);
            updateScale();
        }
    }

    private void updateScale() {
        double scale = m_map.getScaleFromZoomLevel(m_map.getZoomLevel());
        m_text = String.valueOf((int) floor(scale/100));
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int desiredWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_IN, VIEW_WIDTH_INCH, getResources().getDisplayMetrics());
        int desiredHeight = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_IN, VIEW_HEIGHT_INCH, getResources().getDisplayMetrics());

        painter.setTextSize(desiredHeight/2);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = Math.min(desiredWidth, widthSize);
        } else {
            width = desiredWidth;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(desiredHeight, heightSize);
        } else {
            height = desiredHeight;
        }

        rulerRects[0].set(0, height - RULER_STROKE_WIDTH_PX, width, height);
        rulerRects[1].set(0, height - RULER_HEIGHT_PX, RULER_STROKE_WIDTH_PX, height);
        rulerRects[2].set(width - RULER_STROKE_WIDTH_PX, height - RULER_HEIGHT_PX, width, height);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        painter.setColor(Color.BLACK);
        for (Rect r : rulerRects) {
            canvas.drawRect(r, painter);
        }
        float width = painter.measureText(m_text);
        canvas.drawText(m_text, canvas.getWidth()/2 - width/2,
                canvas.getHeight() - painter.getTextSize()/2, painter);
    }

    @Override
    public void onMapTransformStart() {

    }

    @Override
    public void onMapTransformEnd(MapState mapState) {
        updateScale();
    }
}

Встроить масштабное представление в ваш MapFragment

<FrameLayout ...>
...
    <com.here.functionaltestv2.MapScaleView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/map_scale_view"
        android:layout_gravity="start|bottom"
        android:layout_margin="4dp"/>
</FrameLayout>

Прикрепить представление Scale к карте в вашем классе MapFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    m_mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.mapfragment);
    m_mapFragment.init(new OnEngineInitListener() {
        @Override
        public void onEngineInitializationCompleted(Error error) {
            if (error == Error.NONE) {
                ...
                ((MapScaleView) findViewById(R.id.map_scale_view)).
                        setMap(m_mapFragment.getMap());
            }
        }
    }
}
...