【问题标题】:Retrieve distance from visible part of Google map从谷歌地图的可见部分检索距离
【发布时间】:2013-12-06 11:26:20
【问题描述】:

我想用Google Maps 在地图上画一个静态圆圈。当用户捏合时,地图将放大/缩小。

我需要知道地图半径(与圆圈中包含的区域有关)并相应地更改底部的搜索栏。

有人知道如何检索从左到右屏幕边缘的距离的解决方案吗?我在Google Maps API doc 没有找到任何东西。

类似这样的:

【问题讨论】:

    标签: android google-maps google-maps-android-api-2 distance


    【解决方案1】:

    使用 VisibleRegion 可以获得所有角坐标以及中心。

    VisibleRegion vr = mMap.getProjection().getVisibleRegion();
    double left = vr.latLngBounds.southwest.longitude;
    double top = vr.latLngBounds.northeast.latitude;
    double right = vr.latLngBounds.northeast.longitude;
    double bottom = vr.latLngBounds.southwest.latitude;
    

    你可以通过这个计算两个区域的距离

    Location MiddleLeftCornerLocation;//(center's latitude,vr.latLngBounds.southwest.longitude)
    Location center=new Location("center");
    center.setLatitude( vr.latLngBounds.getCenter().latitude);
    center.setLongitude( vr.latLngBounds.getCenter().longitude);
    float dis = center.distanceTo(MiddleLeftCornerLocation);//calculate distane between middleLeftcorner and center 
    

    【讨论】:

    • 获得与顶角的距离会使圆圈大于可见区域的宽度,所以这并不正确
    • 其实我的意思是左中角。我现在已经更正了我的答案
    • 如何计算 MiddleLeftCornerLocation?它是空的
    • tyczj,你可以计算顶部的距离和左侧的距离,选择更小。
    • 如果你缩小到足够的范围,圆圈不会在可见视图的中间,半径也会变得小于宽度/高度的一半,你能解释一下吗?
    【解决方案2】:

    我有时间在@Md 给出的出色答案中编写“MiddleLeftCornerLocation”变量的代码。 Monsur Hossain Tonmoy,所以只是为了完成它(我没有足够的声望点来评论你的答案,对不起):

        VisibleRegion vr = map.getProjection().getVisibleRegion();
        double bottom = vr.latLngBounds.southwest.latitude;
    
        Location center = new Location("center");
        center.setLatitude(vr.latLngBounds.getCenter().latitude);
        center.setLongitude(vr.latLngBounds.getCenter().longitude);
    
        Location middleLeftCornerLocation = new Location("center");
        middleLeftCornerLocation.setLatitude(center.getLatitude());
        middleLeftCornerLocation.setLongitude(left);
    
        float dis = center.distanceTo(middleLeftCornerLocation);
    

    【讨论】:

      【解决方案3】:

      先用

      得到地图中心点
      googleMap.getCameraPosition().target;
      

      然后计算出地图的宽度并获得 height/2 并使用您刚刚获得的 x y 值将它们转换为 Point

      然后将该点与地图相关联

      LatLng widthPoint = map.getProjection().fromScreenLocation(point);
      

      现在您有了 tagret 点和宽度点,您可以计算这两个点之间的距离,这将为您提供半径。

      注意

      这假设始终处于纵向模式,如果您处于横向模式,则圆圈将大于可见区域,因此在这种情况下,您需要获取与高度的距离

      【讨论】:

        猜你喜欢
        • 2014-08-12
        • 1970-01-01
        • 2010-10-28
        • 2023-03-23
        • 1970-01-01
        • 2020-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多