【问题标题】:Android Google Map how to check if user is in marker rectangle regionAndroid Google Map如何检查用户是否在标记矩形区域
【发布时间】:2021-08-20 09:08:12
【问题描述】:

我正在使用 Google 地图,并且希望能够使用用户的当前位置检测用户是否位于标记的矩形(放置在地图上)中。我有用户当前的位置坐标(经纬度)和标记的坐标,但不知道如何计算用户是否在该区域内。

【问题讨论】:

    标签: java android google-maps distance area


    【解决方案1】:

    简短的回答是使用PolyUtil.containsLocation 使用矩形角点(以标记为中心)和用户的位置:

    boolean isWithin = PolyUtil.containsLocation(userPt, rectanglePts, true);
    

    这是map utils 上的文档。还有 PolyUtil java docs

    这是一个演示代码片段。请注意,矩形旋转由实用程序处理,因此无需与坐标系对齐。事实上,它甚至不需要是一个矩形。

    (我选择使用圆圈进行显示,但可以用标记代替。):

    在此示例中,标记被绘制为绿色圆圈,包含矩形和两个任意点(可能的用户位置)以显示计算结果(RED=in,BLUE=out)。

    // Create rectangle coordinates 
    LatLng upperLeft = new LatLng(39.274659,-77.639552);
    LatLng upperRight = SphericalUtil.computeOffset(upperLeft, 12000, 120);
    LatLng lowerRight = SphericalUtil.computeOffset(upperRight, 5000, 210);
    LatLng lowerLeft = SphericalUtil.computeOffset(lowerRight, 12000, 300);
    
    // Make a list for the polygon
    List<LatLng> polygonPts = new ArrayList<>();
        polygonPts.add(upperLeft);
        polygonPts.add(upperRight);
        polygonPts.add(lowerRight);
        polygonPts.add(lowerLeft);
    
    // Draw rectangle
    mMap.addPolygon(new PolygonOptions().addAll(polygonPts).geodesic(true).strokeColor(Color.BLACK));
    
    // Draw marker (center of rectangle)
    LatLng mPos = SphericalUtil.computeOffset(SphericalUtil.computeOffset(upperLeft,6000,120),2500, 210);
    
    // Now add a circle to represent the marker - circles display nicer
    mMap.addCircle(new CircleOptions().center(mPos).fillColor(Color.GREEN).radius(300).strokeWidth(1));
    
    
    // Add two points and compute whether they are inside or outside rectangle and
    // set color based on that (RED=inside BLUE=outside)
    LatLng circleInside = SphericalUtil.computeOffset(mPos,1000, 320);
    LatLng circleOutside = SphericalUtil.computeOffset(mPos,4000, 45);
    
    // Determine if point is inside rectangle and set color accordingly.
    int insideCircleColor = (PolyUtil.containsLocation(circleInside, polygonPts, true) ? Color.RED : Color.BLUE);
    int outsideCircleColor = (PolyUtil.containsLocation(circleOutside, polygonPts, true) ? Color.RED : Color.BLUE);
    
    // Add to map
    mMap.addCircle(new CircleOptions().center(SphericalUtil.computeOffset(circleInside,1000, 320)).fillColor(insideCircleColor).radius(200).strokeWidth(1));
    mMap.addCircle(new CircleOptions().center(SphericalUtil.computeOffset(circleOutside,1000, 320)).fillColor(outsideCircleColor).radius(200).strokeWidth(1));
    
    // and zoom to center
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mPos, 12.0f));
    

    并显示:

    【讨论】:

      【解决方案2】:

      如果它们在框中,则经度和纬度将在边界框的点之间。检查此站点以了解经度和纬度的确切工作原理。 https://gsp.humboldt.edu/olm/Lessons/GIS/01%20SphericalCoordinates/Latitude_and_Longitude.html

      【讨论】:

        猜你喜欢
        • 2022-10-06
        • 2014-04-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-02
        相关资源
        最近更新 更多