【问题标题】:Android Google Map- how to divide map into 4 quadrants and know marker quadrant positionAndroid Google Map-如何将地图划分为4个象限并知道标记象限的位置
【发布时间】:2019-11-25 06:55:51
【问题描述】:

我有一个谷歌地图,我有一些标记。标记已在地图上渲染,并且标记是根据特定类别放置的。这些类别是地图上的象限。

现在用户点击地图上的标记后,我需要知道该标记在哪个象限。

这是我到目前为止所要做的。我以为我会得到这样的 4 个象限:

val screenLoc = googleMap.projection.toScreenLocation(marker.position)

val q1 = googleMap.projection.visibleRegion.farLeft
val q2 = googleMap.projection.visibleRegion.farRight
val q3 = googleMap.projection.visibleRegion.nearLeft
val q4 = googleMap.projection.visibleRegion.nearRight

但我有点卡住了,我怎么知道标记在哪个象限。

【问题讨论】:

  • 你应该知道你的地图边界(NE / SW 坐标)以及你的中心点,那么知道标记是在中线的左上角还是右下角等有什么问题。 ?
  • 您还可以将视口划分为 4 个多边形,然后使用 PolyUtil 类 (containsLocation) googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/… 检查其中是否包含标记
  • 这实际上是个好信息。但如何划分。视口分成 4 个多边形。
  • 正如我所说的...得到你的地图边界,然后你知道西南和东北坐标。获取您的地图中心点,您就知道中间坐标。因此,对于左上角多边形,纬度从顶部(北纬)到中(中纬),经度从左(西 lng)到中(中 lng),依此类推。如果有帮助,您可以检查一下:jsfiddle.net/upsidown/urtuoLsp

标签: android google-maps android-maps-v2


【解决方案1】:

这是一个使用 Javascript API 的工作代码 sn-p。这段代码中没有什么是你不能用 Android API 做的(对不起,我不是在 Android 上开发的)。逻辑完全一样。

我所做的(参见代码中的 cmets)是:

  1. 获取地图边界和中心点
  2. 提取西南和东北坐标
  3. 根据这些坐标创建 4 个矩形
  4. 创建一些标记
  5. 单击标记时,检查它是否在 4 个矩形之一内并输出消息

希望这会有所帮助。

注意:矩形当然可以是透明的,如果你需要一个可见的分割,你可以给矩形设置一个笔触或者用折线分割地图。

var map;
var rectangles = [];

function initialize() {

  var center = new google.maps.LatLng(0, 0);

  var mapOptions = {
    zoom: 10,
    center: center,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

  // Set markers and rectangles once the map is idle
  google.maps.event.addListenerOnce(map, 'idle', function() {

    // Get map bounds, north-east and south-west coords
    var bounds = map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();

    // Create a few markers to test
    setMarker(new google.maps.LatLng(-0.05, -0.05));
    setMarker(new google.maps.LatLng(0.05, -0.05));
    setMarker(new google.maps.LatLng(0.05, 0.05));
    setMarker(new google.maps.LatLng(-0.05, 0.05));

    // Define 4 rectangles based on map bounds and center
    setRectangle(new google.maps.LatLngBounds(
      new google.maps.LatLng(center.lat(), sw.lng()),
      new google.maps.LatLng(ne.lat(), center.lng()),
    ), 'blue');

    setRectangle(new google.maps.LatLngBounds(
      new google.maps.LatLng(center.lat(), center.lng()),
      new google.maps.LatLng(ne.lat(), ne.lng()),
    ), 'yellow');

    setRectangle(new google.maps.LatLngBounds(
      new google.maps.LatLng(sw.lat(), sw.lng()),
      new google.maps.LatLng(center.lat(), center.lng()),
    ), 'green');

    setRectangle(new google.maps.LatLngBounds(
      new google.maps.LatLng(sw.lat(), center.lng()),
      new google.maps.LatLng(center.lat(), ne.lng()),
    ), 'red');
  });

}

function setMarker(latLng, title) {

  var marker = new google.maps.Marker({
    position: latLng,
    map: map,
    title: title,
    draggable: true
  });

  new google.maps.event.addListener(marker, 'click', function() {

    checkRectangleContains(this.position);
  });
}

function setRectangle(bounds, color) {

  var rectangle = new google.maps.Rectangle({
    strokeWeight: 0,
    fillColor: color,
    fillOpacity: 0.35,
    map: map,
    bounds: bounds,
    _reference: color
  });

  rectangles.push(rectangle);
}

function checkRectangleContains(markerCoords) {

  for (var i = 0; i < rectangles.length; i++) {

    // Check if the rectangle bounds contain the marker position
    if (rectangles[i].getBounds().contains(markerCoords)) {

      // Output message
      document.getElementById('rectangle-contains').innerHTML = 'This point is contained within ' + rectangles[i]._reference + ' rectangle.';
    }
  }
}
#map-canvas {
  height: 180px;
}
<div id="map-canvas"></div>
<div id="rectangle-contains"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize">
</script>

【讨论】:

  • 给我几天时间。它看起来合乎逻辑。实施后我会回到这里。我已经采用了你的天才算法。谢谢
  • 嘿,我收到以下错误:java.lang.IllegalArgumentException:南纬超过北纬 (-6.257081466772559 > -6.2602917752466904) 我正在做你所做的事情:val q4 = LatLngBounds(center, sw) val q3 = LatLngBounds(sw, center) val q2 = LatLngBounds(center, ne) val q1 = LatLngBounds(ne, center)
  • 我建议您使用您现在使用的代码打开一个新问题。
  • 它全部固定...android不使用矩形。必须仅使用折线来构建自己的形状,但我已对其进行了排序,谢谢
  • 嘿,我想知道你知道如何使用 quadTree 来实现这个吗? developers.google.com/maps/documentation/ios-sdk/utility/… 告诉我,我可以发布问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
  • 2014-08-27
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
相关资源
最近更新 更多