我正在使用https://developer.here.com/documentation/android-premium/dev_guide/topics/maps.html
中所述的HERE map API的Android Premium SDK我想要实现的是在右下角绘制一个地图比例尺。谁能提供一个示例来实现这一目标?
如果您需要更多信息,请随时在评论中提问。 谢谢 托马斯
答案 0 :(得分:1)
您可以执行以下操作:
创建比例视图(请在您的应用需要的地方进行修改)
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>
附加比例尺视图以映射到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());
}
}
}
}