仅在绘制的圆圈或周长内绘制标记 Google Maps v2 Android [英] Only Plotting Markers Within a Drawn Circle or Perimeter Google Maps v2 Android

查看:29
本文介绍了仅在绘制的圆圈或周长内绘制标记 Google Maps v2 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上标题说明了一切.我不想像现在那样在地图上绘制数据库中的每个条目,而是想查询数据库并只绘制坐标落在用户当前位置周围绘制的圆圈内的条目,但是我不能很清楚怎么做.目前,我已经编写了代码来绘制用户在地图上的当前位置以及存储在我的数据库中的所有条目的位置以及当前位置周围的圆圈(见下图).根据我下面的图片,我只想绘制圆圈内的三个标记.

Basically the title says it all. Rather than plotting every single entry in my database on a map as I do at the moment, I want to query the database and only plot the entries whose co-ordinates fall within the circle drawn around the users current location, however I can't quite figure out how to do it. At the moment, I have written code that plots the users current location on the map along with the locations of all the entries stored on my database and also the circle around the current location (See picture below). Based on my picture below, I only want the three markers within the circle to be plotted.

有谁知道是否有任何方法可以检查存储在我的数据库中的 latlng 坐标是否在圆的区域内?或者如果没有,任何人都可以提出任何使用类似想法的替代方案.

Does anyone know if there are any ways of checking if the latlng co-ordinates stored in my database fall within the area of the circle? Or if not, could anyone suggest any alternatives that use a similar idea.

我想到的另一种方法是使用正方形/矩形而不是圆形,这样我就可以简单地将条目的坐标与正方形/矩形的边界进行比较,但是我真的不知道这样做的可行性被视为 Google Maps API 不支持这些形状.我还遇到了 LatLngBounds 类可能很有用,但我找不到任何使用它的示例代码.我该怎么做?

An alternative I was thinking of was using a square / rectangle rather than a circle so that I could simply compare the co-ordinates of my entries with bounds of the square / rectangle, however I don't really know how feasible that is seen as those shapes aren't supported by the Google Maps API. I also came across the LatLngBounds class which could be useful but I can't find any sample code using it. How I might do this either?

推荐答案

好的,我已经找到了使用我在问题中提到的 LatLngBounds 类的解决方案.它的作用是:

Ok, I've figured out a solution using the LatLngBounds class I referred to in my question. What it does is:

  1. 围绕用户的当前纬度和经度坐标创建一个新的矩形周长(本示例大约为一平方公里).
  2. 然后检查周边是否包含存储在数据库中的坐标.
  3. 如果是,则标记会绘制在地图上.

  1. Creates a new rectangular perimeter around the user's current latitude and longitude co-ordinates (this example is roughly one square kilometer).
  2. It then checks if the perimeter contains the co-ordinates stored in the database.
  3. If it does, the marker is plotted on the map.

public void getNearbyMarkers(){
    LatLngBounds perimeter = new LatLngBounds(new LatLng(currentLat - 0.004,
            currentLon - 0.004), new LatLng(currentLat + 0.004, currentLon + 0.004));


    if (perimeter.contains(LatlongFromDatabase)) {
        //Plot Marker on Map
    } else {
        Toast.makeText(getApplicationContext(), "Co-ordinates not in perimeter!", 8).show();
    }
}

这篇关于仅在绘制的圆圈或周长内绘制标记 Google Maps v2 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆