Query nearby points by longitude and latitude

Source: Internet
Author: User
Tags asin convert radians to degrees

General idea: Based on the given latitude and longitude (Lat, LNG), find the four positions in the upper left corner (left_top), upper right corner (right_top), lower left corner (left_bottom), and lower right corner (right_bottom. All the ranges in this region are near this point.
Reference: http://blog.charlee.li/location-search/

package com.show.common.util;public class Location {    private double latitude;    private double longitude;    public Location(double latitude, double longitude) {        this.latitude = latitude;        this.longitude = longitude;    }    public double getLatitude() {        return latitude;    }    public void setLatitude(double latitude) {        this.latitude = latitude;    }    public double getLongitude() {        return longitude;    }    public void setLongitude(double longitude) {        this.longitude = longitude;    }}
Package COM. show. common. util; public class latitudelontitudeutil {// http://blog.charlee.li/location-search//** /*/Private Static final double earth_radius = 6371000;/** range distance */private double distance; /** upper left corner */private location left_top = NULL;/** upper right corner */private location right_top = NULL;/** lower left corner */private location left_bottom = NULL; /** bottom right corner */private location right_bottom = NULL; PRI Vate latitudelontitudeutil (double distance) {This. distance = distance;} private void getrectangle4point (double Lat, double LNG) {// float dlng = 2 * asin (sin (distance/(2 * earth_radius )) /cos (LAT); // float dlng = degrees (dlng) // converts radians to degrees double dlng = 2 * Math. asin (math. sin (distance/(2 * earth_radius)/math. cos (LAT); dlng = math. todegrees (dlng); // dlat = distance/earth_radius // DL Ng = degrees (dlat) # convert radians to degrees double dlat = distance/earth_radius; dlat = math. todegrees (dlat); // converts radians to degrees // left-top: (LAT + dlat, LNG-dlng) // right-top: (LAT + dlat, LNG + dlng) // left-bottom: (lat-dlat, LNG-dlng) // right-bottom: (lat-dlat, LNG + dlng) left_top = new location (LAT + dlat, LNG-dlng); right_top = new location (LAT + dlat, LNG + dlng); left_bottom = new location (L At-dlat, LNG-dlng); right_bottom = new location (lat-dlat, LNG + dlng);} public static double hav (double theta) {double S = math. sin (theta/2); Return S * s;} public static double getdistance (double lat0, double lng0, double LAT1, double lng1) {// from math import sin, asin, cos, radians, FABS, SQRT // def hav (theta): // s = sin (theta/2) // return S * S // def get_distance_hav (lat0, ln G0, LAT1, lng1): // "The haversine formula is used to calculate the distance between two points of the sphere. "// # Converts longitude and latitude to radians // lat0 = radians (lat0) // LAT1 = radians (LAT1) // lng0 = radians (lng0) // lng1 = radians (lng1) // dlng = FABS (lng0-lng1) // dlat = FABS (lat0-LAT1) // H = hav (dlat) + cos (lat0) * Cos (LAT1) * hav (dlng) // distance = 2 * earth_radius * asin (SQRT (H) // return distance lat0 = math. toradians (lat0); LAT1 = math. toradians (LAT1); lng0 = math. toradians (lng0); lng1 = math. toradians (lng1); double dlng = math. ABS (lng0-lng1); double dlat = math. ABS (lat0-LAT1); double H = hav (dlat) + math. cos (lat0) * Math. cos (LAT1) * hav (dlng); double distance = 2 * earth_radius * Math. asin (math. SQRT (h); Return distance;} public static location [] getrectangle4point (double Lat, double LNG, double distance) {latitudelontitudeutil LlU = new latitudelontitudeutil (distance); LlU. getrectangle4point (Lat, LNG); Location [] Locations = new location [4]; locations [0] = LlU. left_top; locations [1] = LlU. right_top; locations [2] = LlU. left_bottom; locations [3] = LlU. right_bottom; return locations;} public static void main (string [] ARGs) {double lat = 30.500; double lng = 120.500; double distance = 500d; Location [] Locations = latitudelontitudeutil. getrectangle4point (Lat, LNG, distance); string SQL = "select * From place where lat>" + locations [2]. getlatitude () + "and lat <" + locations [0]. getlatitude () + "and LNG>" + locations [0]. getlonglinks () + "and LNG <" + locations [1]. getlongpolling (); system. out. println (SQL); double LAT1 = 30.495503391970406; double lng1 = 120.49261708577215; double D = latitudelontitudeutil. getdistance (Lat, LNG, LAT1, lng1); system. out. println (d );}}

 

Article by: http://0414.iteye.com/blog/2039199

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.