Android Studio 如何获得多边形或折线的名称,如果我的位置在那里?(OSMDroid)

xmq68pz9  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(112)

我与OSMDroid工作,使Map离线在Android工作室。
这是我创建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中创建了多边形和折线。但是现在我想读取那个多边形或折线,如果我的位置在那里的话?如何使它成为可能?

v09wglhw

v09wglhw1#

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

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
    }
  }
}

检查location/GeoPoint是否在给定的多边形内是一个比较复杂的工作,因为唯一的集成方法是基于MotionEvent而不是GeoPoint,并且只在非常特定的场景中返回正确的值,请参阅此作为参考。但也有一些答案,可能会对您的需要有用,例如this one
参考文献:

相关问题