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

查看:161
本文介绍了仅在绘制的圆或周长内绘制标记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.

有人知道是否有任何方法可以检查存储在我的数据库中的纬度坐标是否在圆的范围内?否则,谁能提出任何使用类似想法的替代方案.

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天全站免登陆