我目前正在开发一个跟踪系统来绘制用户在房间内或其他地方的移动轨迹。
现在我已经成功地将我的 map 导入到应用程序中, map 可以自由缩放和移动。 下一步是放置一个标记以指示用户在 map 上的位置,并在 map 上四处移动标记。 (像漂浮在 map 上一样)
我在某处读到它,认为这可以用 MyLocationOverlay 来完成,但是网上的大多数示例都是将它应用到 google map 上。但是,就我而言,我的 map 不是google map,而是我自己的 map 。 ( map 可以是我房间的 map ,也可以是我自己画的 map !)
我已经完成了跟踪算法,即我知道在哪里放置我的标记图像。
因此剩下的唯一问题是如何在 map 图像之上显示标记图像。
具体来说,我有一个自定义 View MapView,如下:
package com.example.drsystem;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
public class MapView extends View {
private static final int INVALID_POINTER_ID = -1;
private Drawable mImage;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
public MapView(Context context) {
this(context, null, 0);
mImage = getResources().getDrawable(R.drawable.lv12n);
mImage.setBounds(0, 0, mImage.getIntrinsicWidth(), mImage.getIntrinsicHeight());
}
// called when XML tries to inflate this View
public MapView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mImage = getResources().getDrawable(R.drawable.lv12n);
mImage.setBounds(0, 0, mImage.getIntrinsicWidth(), mImage.getIntrinsicHeight());
}
public MapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
Log.d("MapView", "X: " + mPosX + " Y: " + mPosY);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
mImage.draw(canvas);
canvas.restore();
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
invalidate();
return true;
}
}
}
至此, map 已经成功输入到app中,并且可以移动和缩放。
然后,我将这个自定义 View 插入到布局 XML 文件中,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MapView" >
<com.example.drsystem.MapView
android:id="@+id/mapView1"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
...
</RelativeLayout>
根据所有这些信息,我如何绘制一个点并不断更新它在我的 map 上的位置?
更新:
问题只是将另一个小图像放在自定义 View 的顶部并四处移动。
我在这里附上截图供大家引用:
按钮和 TextView 上方的区域是我的 MapView
最佳答案
首先,使用 LocationClient API(而不是 LocationManager),因为它们与混合的 gps 供应商 wifi 和网络一起工作得非常好。他们还使用传感器来识别运动,从而节省电池电量。
我使用以下 LocationOverlay cod 将我的图标放置在 map 上。根据您的需要定制它。它有点粗糙但有效。
private class LocationMarker extends ItemizedOverlay<OverlayItem> {
private Activity appContext;
private List<OverlayItem> items = new ArrayList<OverlayItem>();
private Drawable marker = null;
private OverlayItem inDrag = null;
private ImageView dragImage = null;
private int xDragImageOffset = 0;
private int yDragImageOffset = 0;
private int xDragTouchOffset = 0;
private int yDragTouchOffset = 0;
private MapView location;
private boolean pickupOrDrop;
public LocationMarker(Activity newAppContext, MapView newLocation, Drawable newMarker,
ImageView newPoint, boolean newWhichPoint) {
super(newMarker);
this.appContext = newAppContext;
this.location = newLocation;
this.marker = newMarker;
this.dragImage = newPoint;
this.pickupOrDrop = newWhichPoint;
xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();
populate();
}
public void placeMarker(GeoPoint myPoint) {
OverlayItem toDrop = new OverlayItem(myPoint, "", "");
items.add(toDrop);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
public int size() {
return (items.size());
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (pickupOrDrop != whichPoint)
return false;
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;
// Draw temp image
if (action == MotionEvent.ACTION_DOWN) {
Log.d("iTaxeeta:Overlay", "Action Down" + marker.toString());
GeoPoint myPoint = null;
if (items.isEmpty()) {
if (whichPoint == PICKUP) {
myPoint = source = location.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
} else if (whichPoint == DROP) {
myPoint = destination = location.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
}
OverlayItem toDrop = new OverlayItem(myPoint, "", "");
items.add(toDrop);
populate();
}
for (OverlayItem item : items) {
Point p = new Point(0, 0);
Log.d("iTaxeeta:Overlay", item.getTitle());
location.getProjection().toPixels(item.getPoint(), p);
if (hitTest(item, marker, x - p.x, y - p.y)) {
result = true;
inDrag = item;
items.remove(inDrag);
populate();
xDragTouchOffset = 0;
yDragTouchOffset = 0;
setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);
xDragTouchOffset = x - p.x;
yDragTouchOffset = y - p.y;
break;
}
else {
items.clear();
populate();
if (whichPoint == PICKUP)
myPoint = source = location.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
else if (whichPoint == DROP)
myPoint = destination = location.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
new GetGeoAddress((IActionBar) appContext, myPoint).execute();
OverlayItem toDrop = new OverlayItem(myPoint, "", "");
items.add(toDrop);
populate();
if (source != null && destination != null) {
searchCabs.setEnabled(true);
// tip.startAnimation(rollUpAnimation);
searchCabs.setTextColor(Color.parseColor("#eeeee4"));
if (whichPoint == PICKUP)
searchCabs.setBackgroundColor(Color.parseColor("#fe000a"));
else if (whichPoint == DROP)
searchCabs.setBackgroundColor(Color.parseColor("#17ee27"));
}
break;
}
}
}
// Draw temp image while moving finger across the screen
else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
Log.d("iTaxeeta:Overlay", "Action Move" + marker.toString());
setDragImagePosition(x, y);
result = true;
}
// Position the selected point now
else if (action == MotionEvent.ACTION_UP && inDrag != null) {
GeoPoint myPoint = null;
Log.d("iTaxeeta:Overlay", "Action Up" + marker.toString());
dragImage.setVisibility(View.VISIBLE);
if (whichPoint == PICKUP)
myPoint = source = location.getProjection().fromPixels(x - xDragTouchOffset,
y - yDragTouchOffset);
else if (whichPoint == DROP)
myPoint = destination = location.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
OverlayItem toDrop = new OverlayItem(myPoint, inDrag.getTitle(),
inDrag.getSnippet());
items.add(toDrop);
populate();
inDrag = null;
new GetGeoAddress((IActionBar) appContext, myPoint).execute();
if (source != null && destination != null) {
searchCabs.setEnabled(true);
// tip.startAnimation(rollUpAnimation);
searchCabs.setTextColor(Color.parseColor("#eeeee4"));
if (whichPoint == PICKUP)
searchCabs.setBackgroundColor(Color.parseColor("#fe000a"));
else if (whichPoint == DROP)
searchCabs.setBackgroundColor(Color.parseColor("#17ee27"));
}
result = true;
}
return (result || super.onTouchEvent(event, mapView));
}
关于android - 如何绘制一个小标记图像并不断刷新其在我的自定义 map 图像上的位置? - 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17481785/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>