【问题标题】:Draw a Circle GeoFence with Preview in Android在 Android 中使用预览绘制圆形 GeoFence
【发布时间】:2014-12-17 14:27:43
【问题描述】:

首先,我的问题与其他关于 Android 中 gmap 中 GeoFencing 的问题有点不同。

当用户开始将圆定义为 GeoFence 区域时,我想在屏幕中心显示一个固定的圆。在这个阶段,用户应该能够移动地图和地图的缩放级别。移动动作和放大/缩小动作都不能改变圆圈相对于屏幕的大小。

当用户按下保存按钮时,我想将该圆圈作为 CircleOption 对象固定到地图上。然后我将能够以米为单位获得 CircleOption 对象的 lon/lat 和半径。

我目前正在处理 RelativeLayout 问题。正如我所期望的那样,我用RelativeLayout 绘制了一个固定的圆,但是我无法获得该RelativeLayout 圆的当前半径。你可以看到我现在的 RelativeLayout 是什么样子的:

我不确定这样做的正确方法。我的搜索重定向我使用RelativeLayout,然后使用CircleOption。你有什么建议?

提前致谢!

【问题讨论】:

  • Circle 是恕我直言的方法。它拥有你所需要的一切:半径、中心、填充颜色等。只需使用 Circle 作为标记比自定义 RelativeLayout IMGO 更好。

标签: android google-maps android-layout geofencing android-geofence


【解决方案1】:
https://developers.google.com/android/reference/com/google/android/gms/maps/model/Circle


CircleOptions circleOptions = new CircleOptions()
  .center( new LatLng(fence.getLatitude(), fence.getLongitude()) )
  .radius( fence.getRadius() )
  .fillColor(0x40ff0000) <Your Color code>
  .strokeColor(Color.TRANSPARENT)
  .strokeWidth(2);

【讨论】:

【解决方案2】:

一种方法是在 XML 的相对布局中定义一个圆圈。这将由用户定义,而不是地图可以在其下方移动。获取圆心作为整个地图片段布局的中心坐标,并且由于您已经定义了圆的半径,您知道圆下扫过的区域是什么。

前提是用户不能缩放或缩放地图。

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    已编辑。 如果我正确理解您的问题,那么问题是如何将 RelativeLayout 坐标转换为地理围栏的半径米? 试试这个i want to convert google map pixels to kilometers 更好的方法是使用

        android.graphics.Point centerPoint = new android.graphics.Point(100, 100);//center of Circle on the screen
        LatLng centerLatLang = mMap.getProjection().fromScreenLocation(centerPoint);
        int radius = 50;//Radius of the circle on the screen
        android.graphics.Point radiusPoint = new android.graphics.Point(centerPoint.x+radius, centerPoint.y);
        LatLng radiusLatLang = mMap.getProjection().fromScreenLocation(radiusPoint);
    
        float[] results = new float[1];
    
        Location.distanceBetween(centerLatLang.latitude, centerLatLang.longitude, radiusLatLang.latitude, radiusLatLang.longitude, results);
    
        float radiusInMeters = results[0];
    

    如果你想将 fron latLang 转换为使用算法的逆点

    mMap.getProjection().toScreenLocation(latLng)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 2020-12-28
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多