C # based on a given latitude and longitude point and distance, search for a location within 5 kilometers nearby,

Source: Internet
Author: User
Tags asin

C # based on a given latitude and longitude point and distance, search for a location within 5 kilometers nearby,

Search nearby locations within 5 kilometers based on a given latitude and longitude point and distance-use algorithms reasonably

Functional requirements: You need to query other sites within a 5-kilometer range (the longitude and latitude of the site are known.

Method 1: Traverse each record and calculate the distance from each vertex in the database. When the distance is less than 5 kilometers, matching is considered (extremely inefficient and time-consuming ).

Method 2: The approximate latitude and longitude range is filtered out Before calculation. Although we are looking for a circle, we can first find the external square of the circle (for example, the four points of the square), first calculate the longitude and latitude of the four points of the rectangle around the point, then, use the longitude and latitude of the square to directly match the records in the database, and then useMethod 1Filtering (compared to using method 1 only, the efficiency is much higher.-Reasonable Use of Algorithms).

 

Public static class DistanceHelper {/// <summary> /// based on a given latitude and longitude point and distance, query nearby locations /// </summary> /// <param name = "longpolling"> longitude </param> /// <param name = "latitude"> latitude </param> // <param name = "distance"> distance (unit: kilometers or kilometers) </param> /// <returns> returns four points in a range, with the minimum latitude and latitude, maximum longitude and latitude </returns> public static PositionModel FindNeighPosition (double longpolling, double latitude, double distance) {// calculate the longitude and latitude range of the query point double r = 6378.137; // Earth's radius km double dis = distance; // km distance from double dlng = 2 * Math. asin (Math. sin (dis/(2 * r)/Math. cos (latitude * Math. PI/180); dlng = dlng * 180/Math. PI; // convert angle to radian double dlat = dis/r; dlat = dlat * 180/Math. PI; double minlat = latitude-dlat; double maxlat = latitude + dlat; double minlng = longyun-dlng; double maxlng = longyun+ dlng; return new PositionModel {MinLat = minlat, maxLat = maxlat, MinLng = minlng, MaxLng = maxlng };}/// <summary> // calculates the distance between two points, and returns the distance between two points. Unit: km or KM // This formula is provided by GOOGLE, the error is less than 0.2 meter//</summary> /// <param name = "lat1"> latitude at the first vertex </param> /// <param name = "lng1"> first point longitude </param> /// <param name = "lat2"> second point latitude </param> /// <param name = "lng2"> second point longitude </param> /// <returns> returns the distance between two points, unit: km or KM </returns> public static double GetDistance (double lat1, double lng1, double lat2, double lng2) {// Earth radius, unit: Meters double EARTH_RADIUS = 6378137; double radLat1 = Rad (lat1); double radLng1 = Rad (lng1); double radLat2 = Rad (lat2); double radLng2 = Rad (lng2); double a = radLat1-radLat2; double B = radLng1-radLng2; double result = 2 * Math. asin (Math. sqrt (Math. pow (Math. sin (a/2), 2) + Math. cos (radLat1) * Math. cos (radLat2) * Math. pow (Math. sin (B/2), 2) * EARTH_RADIUS; return result/1000 ;} /// <summary> /// converts the longitude and latitude to radians /// </summary> /// <param name = "d"> </param> /// <returns> </returns> private static double Rad (double d) {return (double) d * Math. PI/180d ;}}

 

If you have a better method, please leave a message. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.