使用经度和纬度查找给定距离内的所有附近客户 [英] Find all nearby customers within a given distance using longitude and latitude

查看:173
本文介绍了使用经度和纬度查找给定距离内的所有附近客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含客户经纬度的数据库,我有一个搜索表单,用户在其中输入日志/纬度和距离下拉列表中包含50英里,100英里,....当用户点击搜索我想写一个linq查询来获取距离这个距离半径的db中的所有客户。如何使用C#和LINQ做到这一点?

I have a db contain customers longitude and latitude, I have a search form in which the user will enter log/lat and distance dropdown contain 50miles, 100miles,.... and when the user click search I want to write a linq query to get all customers from the db which are in this distance radius. How to make this using C# and linq?

更新:

我发现这个https://stackoverflow.com/a/1654365/20126 ,但这给出了一个不是半径的方形

Update:
I found this https://stackoverflow.com/a/1654365/20126 but this gives a square not radius

推荐答案

对类似问题的回答稍作修改:

A little modification of my answer to a similar question:

// radius is the distance in meters
var center = new GeoCoordinate(latitude, longitude);
var result = customers.Select(x => new GeoCoordinate(x.Latitude, x.Longitude))
                      .Where(x => x.GetDistanceTo(center) < radius);

您需要添加对 System.Device.dll

这篇关于使用经度和纬度查找给定距离内的所有附近客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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