【问题标题】:How to get name of polygon or polyline if my location inthere ? (OSMDroid)如果我的位置在那里,如何获取多边形或折线的名称? (OSMD机器人)
【发布时间】:2022-10-20 10:07:34
【问题描述】:

我正在使用 OSMDroid 在 Android Studio 中制作离线地图。

这是我创建 Polygon 的代码:

polygon = new Polygon();
polygon.setPoints(list_polygon);
polygon.setFillColor(Color.parseColor("#1D566E"));
polygon.setTitle(name_map);
polygon.getOutlinePaint().setColor(polygon.getFillPaint().getColor());
map.getOverlays().add(polygon);

此代码是创建行:

line = new Polyline();
line.setPoints(list_line);
line.setGeodesic(true);
line.setColor(Color.parseColor("#E33E5A"));
line.getOutlinePaint().setStrokeWidth(30f);
line.setWidth(15f);
line.getPaint().setStrokeCap(Paint.Cap.ROUND);
map.getOverlays().add(line);

这段代码是为了获取我的位置:

myLocation = new MyLocationNewOverlay(map);
myLocation.enableFollowLocation();
myLocation.setDirectionArrow(icTruk, icTruk);
myLocation.enableMyLocation();
myLocation.setDrawAccuracyEnabled(true);
map.getOverlays().add(myLocation);

我已经在 osmdroid 中创建了多边形和折线。 但是现在我想读取那个多边形或折线,如果 mylocation 在那里? 如何使它成为可能?

【问题讨论】:

    标签: java android android-studio openstreetmap osmdroid


    【解决方案1】:

    您可以执行以下操作来获取当前位置,然后检查它是否靠近折线。

    MyLocationNewOverlay myLocation= new MyLocationNewOverlay(mapView) {
      @Override
      public void onLocationChanged(Location location, IMyLocationProvider source) {
        super.onLocationChanger(location, source);
    
        // Turn the current location into a GeoPoint
        GeoPoint currentPoint = new GeoPoint(location.getLatitude(), location.getLongitude);
    
        // Set tolerance for isCloseTo 
        // Might need to play around with this value 
        // and see which fits best for your needs
        double tolerance = 5.0;
    
        // Check if location is close to Polyline
        if (line.isCloseTo(currentPoint, tolerance, map)) {
            // Do here what you want to do, 
            // when location is close to Polyline
        }
      }
    }
    

    检查位置/GeoPoint 是否在给定的多边形内需要更多的工作,因为唯一的集成方法是基于 MotionEvent 而不是 GeoPoint,并且仅在非常特定的场景中返回正确的值,请参阅 this 以供参考.但是那里有一些答案,可能对您的需要有用,例如this one

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-20
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      相关资源
      最近更新 更多