图形页面上添加触摸图钉 [英] MapView adding pushpins on touch
问题描述
我设法在屏幕上显示的地图。现在,用户将围绕它的地图和preSS移动。经过上的一个点pressing我需要添加一个图钉在屏幕上的pressed位置。如果用户决定去另外一点,当所选择的点pressing,第一个图钉会disapear和新将在新的位置绘制
I managed to get the map shown on the screen. Now the user will move around the map and press on it. After pressing on a point I need to add a push pin on screen on the pressed location. If the user decides to go for another point, when pressing on the chosen point, the first pushpin would disapear and a new one will be drawn on the new location
我不喜欢这样的:
public class LocationSelectionActivity extends MapActivity
{
GeoPoint p;
List<Overlay> listOfOverlays;
MapOverlay mapOverlay;
private MapView mapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.locationselection);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setSatellite(false);
final MapController mc = mapView.getController();
String coordinates[] = {"46.540606", "22.454542"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(10);
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomOut();
}
});
//---Add a location marker---
mapOverlay = new MapOverlay();
listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.map_flag);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-48, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
}
此画最初的图钉(该map_flag资源)。当我得到的吐司与coordonates在地图上点击......一切都剩下要做的就是在一个新的点击,擦除旧针,并添加一个新的。
This draws the initial pushpin (the map_flag resource). When clicking on the map I get the Toast with the coordonates... all is left to do is on a new click, erase the old pin and add a new one.
但怎么办呢?
推荐答案
我会使用 ItemizedOverlay
,而不是一个常规重叠
。然后,所有你需要做的就是更新您的 OverlayItem
的对象集合,并调用填充()
在 ItemizedOverlay
。
I would use an ItemizedOverlay
, rather than a regular Overlay
. Then, all you need to do is update your collection of OverlayItem
objects and call populate()
on the ItemizedOverlay
.
下面是 呈现出示例项目拖使用针 ItemizedOverlay
的-drop。
Here is a sample project showing drag-and-drop of a pin using ItemizedOverlay
.
这篇关于图形页面上添加触摸图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!