在android中点击地图获取坐标
我正在尝试做这样的事情:我有一个地图活动,当用户点击地图时,它会显示该位置的坐标。我已经重写了 onclick 方法,但它甚至没有被调用。有什么想法吗?
public class MapPoint extends MapActivity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mappoints);
MapView map = (MapView)findViewById(R.id.mapview);
map.setOnClickListener(this);
map.setBuiltInZoomControls(true);
map.getController().setZoom(18);
map.getController().setCenter(new GeoPoint(39735007, -8827330));
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View arg0) {
Toast.makeText(this, "text", Toast.LENGTH_SHORT);
}
}
I'm trying to make something like this: I have a mapactivity and when the user taps the map it shows the coordinates of that location. I already overrided the onclick method but it isn't even called. Any Idea?
public class MapPoint extends MapActivity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mappoints);
MapView map = (MapView)findViewById(R.id.mapview);
map.setOnClickListener(this);
map.setBuiltInZoomControls(true);
map.getController().setZoom(18);
map.getController().setCenter(new GeoPoint(39735007, -8827330));
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View arg0) {
Toast.makeText(this, "text", Toast.LENGTH_SHORT);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试以下操作。
编写一个派生自 覆盖 类并覆盖 onTap()方法。然后,您可以将叠加层添加到您的 MapView.
Try the following.
Write a class which derives from the Overlay class and override the onTap() method. Then you can add your overlay to the your MapView. A GeoPoint object, which represents the position of you tap, is passed to the onTap() method when you tab somewhere on the map.
这里没有使用
onClick()
。您需要重写onTouchEvent()
。 这是一个示例项目,显示使用onTouchEvent( )
拖放ItemizedOverlay
地图图钉。给定触摸的屏幕坐标,您可以使用
Projection
(来自MapView
上的getProjection()
)将其转换为纬度和经度。onClick()
is not used here. You will need to overrideonTouchEvent()
. Here is a sample project showing usingonTouchEvent()
to drag and dropItemizedOverlay
map pins.Given the screen coordinates of the touch, you can use a
Projection
(fromgetProjection()
onMapView
) to convert that to latitude and longitude.使用 Android Maps v2 的现代答案是使用 OnMapClickListener,它为您提供地图上点击的 LatLng。
https://developer.android.com/reference /com/google/android/gms/maps/GoogleMap.OnMapClickListener.html
The modern answer, using Android Maps v2, is to use OnMapClickListener, which gives you the LatLng of a tap on the map.
https://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.OnMapClickListener.html
我在这个问题中添加了一些实现细节:
怎么办我响应 Android MapView 上的点击,但忽略捏缩放?
我发现使用 onTap() 的问题是,当您捏缩放时它也会被触发,这不是我的应用程序所需要的。因此,如果您需要捏合缩放,我的代码应该可以满足您的需要。
I put some details of my implementation in this question:
How do I respond to a tap on an Android MapView, but ignore pinch-zoom?
The problem I found with using onTap() is that it also gets fired when you pinch-zoom, which was not what my app needed. So if you need pinch-zoom, my code should do what you need.
试试这个:
Try this:
就像@CommonsWare所说,您需要使用
onTouch()
函数而不是onClick()
。这是一个示例,这正是您所需要的: http://mobiforge.com /developing/story/using-google-maps-androidLike @CommonsWare said, you need to use
onTouch()
function and notonClick()
. Here is an example, this is exactly what you need : http://mobiforge.com/developing/story/using-google-maps-android