MapView 在触摸时添加图钉
我设法让地图显示在屏幕上。现在,用户将在地图上移动并按下它。按下一个点后,我需要在屏幕上按下的位置添加一个图钉。如果用户决定去另一个点,当按下所选点时,第一个图钉将消失,并且将在新位置上绘制一个新的图钉,
我这样做是这样的:
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资源)。单击地图时,我会看到带有坐标的 Toast...剩下要做的就是再次单击,删除旧的图钉并添加新的图钉。
但怎么办呢?
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
I did like this:
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;
}
}
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.
But how to do it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用
ItemizedOverlay
,而不是常规的Overlay
。然后,您需要做的就是更新OverlayItem
对象集合并在ItemizedOverlay
上调用populate()
。这是示例项目,显示了拖放功能使用
ItemizedOverlay
的图钉。I would use an
ItemizedOverlay
, rather than a regularOverlay
. Then, all you need to do is update your collection ofOverlayItem
objects and callpopulate()
on theItemizedOverlay
.Here is a sample project showing drag-and-drop of a pin using
ItemizedOverlay
.这段代码工作正常。
This code is working fine .
这会起作用。使 OnTouchEvent 的 GeoPoint 公开...
This will work. Make OnTouchEvent's GeoPoint Public...