在android中点击地图获取坐标

发布于 2024-10-02 04:41:34 字数 774 浏览 4 评论 0原文

我正在尝试做这样的事情:我有一个地图活动,当用户点击地图时,它会显示该位置的坐标。我已经重写了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

莫多说 2024-10-09 04:41:34

尝试以下操作。

编写一个派生自 覆盖 类并覆盖 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.

怕倦 2024-10-09 04:41:34

这里没有使用onClick()。您需要重写onTouchEvent()这是一个示例项目,显示使用 onTouchEvent( ) 拖放 ItemizedOverlay 地图图钉。

给定触摸的屏幕坐标,您可以使用 Projection(来自 MapView 上的 getProjection())将其转换为纬度和经度。

onClick() is not used here. You will need to override onTouchEvent(). Here is a sample project showing using onTouchEvent() to drag and drop ItemizedOverlay map pins.

Given the screen coordinates of the touch, you can use a Projection (from getProjection() on MapView) to convert that to latitude and longitude.

南薇 2024-10-09 04:41:34

使用 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

街角迷惘 2024-10-09 04:41:34

我在这个问题中添加了一些实现细节:
怎么办我响应 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.

南…巷孤猫 2024-10-09 04:41:34

试试这个:

private class OverlayMapa extends Overlay {

 @Override
 public void draw(Canvas canvas, MapView mapView, boolean shadow) 
 {
   ...
 }

 @Override
 public boolean onTap(GeoPoint point, MapView mapView) 
 {
   Context contexto = mapView.getContext();
   String msg = "Lat: " + point.getLatitudeE6()/1E6 + " - " + 
                "Lon: " + point.getLongitudeE6()/1E6;

   Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
   toast.show();

  return true;
 }
}

Try this:

private class OverlayMapa extends Overlay {

 @Override
 public void draw(Canvas canvas, MapView mapView, boolean shadow) 
 {
   ...
 }

 @Override
 public boolean onTap(GeoPoint point, MapView mapView) 
 {
   Context contexto = mapView.getContext();
   String msg = "Lat: " + point.getLatitudeE6()/1E6 + " - " + 
                "Lon: " + point.getLongitudeE6()/1E6;

   Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
   toast.show();

  return true;
 }
}
白云不回头 2024-10-09 04:41:34

就像@CommonsWare所说,您需要使用onTouch()函数而不是onClick()。这是一个示例,这正是您所需要的: http://mobiforge.com /developing/story/using-google-maps-android

Like @CommonsWare said, you need to use onTouch() function and not onClick(). Here is an example, this is exactly what you need : http://mobiforge.com/developing/story/using-google-maps-android

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文