在给定半径内在地图上显示标记(Xamarin.Android) [英] Show markers on map within a given radius (Xamarin.Android)

查看:209
本文介绍了在给定半径内在地图上显示标记(Xamarin.Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要实现的方案:

Here is the scenario that I need to implement:

假设我有一个数据库表,其中包含我城市所有 个餐厅的纬度和经度,例如500个餐厅.现在,我正使用我的应用程序站在城市中的某个地方,想知道附近的餐馆,比如说方圆10英里以内.

Assume that I have a database table that contains Latitudes and Longitudes of all the restaurants of my city, say 500 restaurants. Now I am standing somewhere in the city using my app and want to know my nearby restaurants, say within 10 miles radius.

要实现此目的,应用程序必须首先知道我的当前位置(我已经做到了).知道我的位置后,应用程序需要在10英里以内的所有餐厅上放置标记(假设500家餐厅中只有25家).我不需要显示比这更远的标记.现在,当我单击任何标记时,我将看到信息窗口",然后单击它,我需要导航到一个新活动并显示一些详细信息,例如地址,姓名,电话号码(所有这些都来自数据库)及其与我当前位置的距离

To achieve this, the app must first know my current location (I have done that). Once my location is known, the app needs to put markers on all the restaurants that are within 10 miles (assume only 25 out of the total 500). I do not need to show the markers for those which are farther than that. Now when I click on any marker I will see the InfoWindow and then clicking on it I need to navigate to a new activity and show some details like address, name, phone number (these all come from database) and its distance from my current location.

现在我有两个问题:

  1. 我只需要显示给定半径内的标记,而不显示其余标记.我也不想在地图上画一个圆.我是否必须遍历所有位置并查看位于半径范围内的位置,还是有一种更快,更有效的方法?
  2. 我可以选择获取所有标记的直线和道路距离吗?

推荐答案

在BI中编写一个方法,该方法为您提供地图边界之间(或距地图中心(LatLon)给定距离内)的所有餐厅. 我曾经这样做是在Sqlite中嵌入自定义函数

Write a method in your BI that gives you all the restaurants between the bounds of the Map (or within a given distance from the center (LatLon) of the Map. I used to do it embedding a custom function in Sqlite

[SqliteFunctionAttribute(Name = "distance", Arguments = 4, FuncType = FunctionType.Scalar)]
class SqliteDistance : SqliteFunction
{
    public override object Invoke(object[] args)
    {
       double latA = System.Convert.ToDouble(args[0]);
       double lonA = System.Convert.ToDouble(args[1]);
       double latB = System.Convert.ToDouble(args[2]);
       double lonB = System.Convert.ToDouble(args[3]);



       const double R = 6371;
       const double pigreco = 3.1415927;
       double lat_alfa, lat_beta;
       double lon_alfa, lon_beta;
       double fi;
       double p, d;
       /* Converte i gradi in radianti */
       lat_alfa = pigreco * latA / 180;
       lat_beta = pigreco * latB / 180;
       lon_alfa = pigreco * lonA / 180;
       lon_beta = pigreco * lonB / 180;
       /* Calcola l'angolo compreso fi */
       fi = Math.Abs(lon_alfa - lon_beta);
       /* Calcola il terzo lato del triangolo sferico */
       p = Math.Acos(Math.Sin(lat_beta) * Math.Sin(lat_alfa) +
         Math.Cos(lat_beta) * Math.Cos(lat_alfa) * Math.Cos(fi));
       /* Calcola la distanza sulla superficie 
       terrestre R = ~6371 km */
       d = p * R;
       return (d);

    }

}

然后在您的查询中:

Public List<Restaurant> GiveMeRestaurants(double fromLat, double fromLon)
{
String sql="Select * from restaurants where distance('" + fromLat.ToString() + "','" + fromLon.ToString() + "',restaurants.Latitude,restaurants.Longitude) <= 100 )";


}

这也可以给您直线距离.

This can give you also the straight line distance.

  1. 对于道路距离,也许您可​​以使用中心地图和坐标或餐厅致电Google Play服务

这篇关于在给定半径内在地图上显示标记(Xamarin.Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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