作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我将 OSM 用于 map 应用程序,我在其中按此处说明的行进方向旋转 map Android Rotating MapView .这很好用。
但是,我还没有设法调整 dispatchTouchEvent 代码来对抗用户触摸的 map 旋转效果(现在本地图旋转 90 度时,用户的水平扫动将移动 map 垂直等)。示例代码仅提供预告片:
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO: rotate events too
return super.dispatchTouchEvent(ev);
}
如有任何指导,我们将不胜感激。
当我在做的时候 - 是否仍然可以定位缩放单独控制,以便它们在 map 旋转时不旋转?我读到 getZoomControls() 已弃用。 (为什么?)
最佳答案
我知道为时已晚,但它可能会帮助某人...
导入 org.osmdroid.views.overlay.MyLocationOverlay;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.Matrix ;
import android.hardware.Sensor;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
/**
* Rotate the Map in accordance to the movement of user and always point to North
*
*/
public class RotatingRelativeLayout extends RelativeLayout
{
private Matrix mMatrix = new Matrix();
private float[] mTemp = new float[2];
private Context context;
private static final float SQ2 = 1.414213562373095f;
public RotatingRelativeLayout(final Context pContext,
final AttributeSet pAttrs)
{
super(pContext, pAttrs);
this.context=pContext;
}
@Override
protected void dispatchDraw(Canvas canvas) {
long rotateTime = MyLocationOverlay.getTimeOfMovement();
float overlayBearing = MyLocationOverlay.getBearing();//this method returns current bearing from OSM
long currentTime = System.currentTimeMillis();
long diffTime = currentTime-rotateTime;
/*
* Here we rotate map in accordance with Compass to point always North
*
*/
if(diffTime >= (40*1000 )){
//isBearing=false;
overlayBearing=0;
canvas.rotate(overlayBearing, getWidth() * 0.5f, getHeight() * 0.5f);
}
else
/*
* Rotate Map According to the user movement
*/
canvas.rotate(-overlayBearing, getWidth() * 0.5f, getHeight() * 0.5f);
canvas.getMatrix().invert(mMatrix);
final float w = this.getWidth();
final float h = this.getHeight();
final float scaleFactor = (float)(Math.sqrt(h * h + w * w) / Math.min(w, h));
canvas.scale(scaleFactor, scaleFactor, getWidth() * 0.5f, getHeight() * 0.5f);
super.dispatchDraw(canvas);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.restore();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int width = getWidth();
final int height = getHeight();
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View view = getChildAt(i);
final int childWidth = view.getMeasuredWidth();
final int childHeight = view.getMeasuredHeight();
final int childLeft = (width - childWidth) / 2;
final int childTop = (height - childHeight) / 2;
view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
int sizeSpec;
if (w > h) {
sizeSpec = MeasureSpec.makeMeasureSpec((int) (w*SQ2), MeasureSpec.EXACTLY);
}
else {
sizeSpec = MeasureSpec.makeMeasureSpec((int) (h*SQ2), MeasureSpec.EXACTLY);
}
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(sizeSpec, sizeSpec);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final float[] temp = mTemp;
temp[0] = event.getX();
temp[1] = event.getY();
mMatrix.mapPoints(temp);
event.setLocation(temp[0], temp[1]);
return super.dispatchTouchEvent(event);
}
}
现在只需像这样在您的 xml 中使用此相对布局:-
<YourPackageName.RotatingRelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/rotating_layout" android:layout_marginBottom="40dip"/>
然后像这样以编程方式添加 map View :-
// Find target container
final RelativeLayout rl=(RelativeLayout)mParent.findViewById(R.id.rotating_layout);
// Create rotator
mRotator=new RotatingRelativeLayout(mParent, null);
// Add map to the rotating layout
// Add rotator to the screen
rl.addView(mMap,new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
这里mParent就是Context。还有一件事,如果你遇到图像像素化问题,你只需要使用它
//Paint distortion handling..
p.setFilterBitmap(true);
希望我能尽可能地解释清楚......如果你发现理解它有问题,请随时询问。
谢谢。
关于android - 旋转的 OpenStreetMap View - 如何在 Android 中旋转后沿手指移动方向滑动 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8668901/
我是一名优秀的程序员,十分优秀!