如何添加触摸事件,显示经纬度在OSM举杯经纬度、事件、OSM

2023-09-04 23:16:21 作者:男神在韩国

我如何低于我的code添加这个功能呢?网址是这样的:how添加更多的标记在OSM地图android的,请帮助我的?我想补充功能打印,其中用户在敬酒的消息触摸屏上显示的经纬度在地图上的每个位置的吐司消息

 公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面){

    如果(event.getAction()== 1){
        。的GeoPoint的GeoPoint =(的GeoPoint)MapView.getProjection()在fromPixels((int)的event.getX(),(int)的event.getY());
        // 纬度
        双纬度= geopoint.getLatitudeE6()/ 1E6;
        // 经度
        双LON = geopoint.getLongitudeE6()/ 1E6;
        Toast.makeText(背景下,纬度:+纬度+,经度:+ LON,Toast.LENGTH_SHORT).show();
    }
    返回false;
}
 

解决方案

使用这块code和创建这个类的一个对象,并把它添加到mapsOverlay

 类MapOverlay扩展com.google.android.maps.Overlay
{
    @覆盖
    公共布尔平局(画布油画,图形页面图形页面,
    布尔的影子,时长)
    {
       // ...
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面)
    {
        // ---当用户抬起手指---
        如果(event.getAction()== MotionEvent.ACTION_UP){
            的GeoPoint P = MapView.getProjection()在。fromPixels(
                (int)的event.getX(),
                (int)的event.getY());
                Toast.makeText(getBaseContext(),
                    p.getLatitudeE6()/ 1E6 +,+
                    p.getLongitudeE6()/ 1E6,
                    Toast.LENGTH_SHORT).show();
        }
        返回false;
    }
}
 
Android触摸事件 笔记篇

添加此行

 调用MapView.getOverlays()增加(新MapOverlay())。
 

How do I add this function below in my code? The url is this: how to add more marker in osm map in android please help me? I want to add feature to print toast message of every location where user touches on the screen and displays its latitude and longitude on map in toast message

public boolean onTouchEvent(MotionEvent event, MapView mapView) {   

    if (event.getAction() == 1) {                
        GeoPoint geopoint = (GeoPoint) mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
        // latitude
        double lat = geopoint.getLatitudeE6() / 1E6;
        // longitude
        double lon = geopoint.getLongitudeE6() / 1E6;
        Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
    }                            
    return false;
} 

解决方案

Use this piece of code and create an object of this class and add it to the mapsOverlay

class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    {
       //...
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == MotionEvent.ACTION_UP) {                
            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;
    }        
}

add this line

mapView.getOverlays().add(new MapOverlay());