Javascript-how to obtain the latitude and longitude of a user and calculate the distance between two locations?

Source: Internet
Author: User
Tags geoip
How can I obtain the longitude and latitude of my current location? After knowing the longitude and latitude of the two locations, how can we calculate the distance between the two locations? Recently I am working on a public account. I need to obtain the user's latitude and longitude, and then calculate the distance between the user's latitude and longitude and the other location? How can I implement this? Please be large... how do I obtain the longitude and latitude of my current location? After knowing the longitude and latitude of the two locations, how can we calculate the distance between the two locations?
Recently I am working on a public account. I need to obtain the user's latitude and longitude, and then calculate the distance between the user's latitude and longitude and the other location? How can I implement this? Please help me. thank you!

Reply content:

How can I obtain the longitude and latitude of my current location? After knowing the longitude and latitude of the two locations, how can we calculate the distance between the two locations?
Recently I am working on a public account. I need to obtain the user's latitude and longitude, and then calculate the distance between the user's latitude and longitude and the other location? How can I implement this? Please help me. thank you!

PHP implements the "nearby person" function, determines the latitude and longitude based on the IP address, and calculates the distance based on the latitude and longitude.

Install GeoIP extensions and databases in PHP. obtain information about the visitor's country, city, longitude, and latitude based on the IP address.
Then you can usegeoip_record_by_name($_SERVER['REMOTE_ADDR'])The longitude and latitude are determined based on the user IP address.
Note: The western longitude and latitude returned by geoip_record_by_name () are negative.

5000 meters to latitude and longitude:
Latitude: 1 deg = 110852 m
Longitude longpolling: 1 deg = 111320 * cos (lat) m
The same longitude line has a latitude difference of about 110852 meters.
On the same latitude line, the difference is about 111320 x cos (lat) meters (lat is the latitude of the weft)


  = ($ Lat-$ y) and lat <= ($ lat + $ y) and lon> = ($ lon-$ x) and lon <= ($ lon + $ x );';

In the database user table, two fields are set to store the user's longitude lat and latitude lon.
($ Lat-$ y) <= lat <= ($ lat + $ y)
($ Lon-$ x) <= lon <= ($ lon + $ x)
This range is a rough range. after calculating the distance below, you can remove users more than 5 kilometers.

Based on the longitude and latitude of the user found above,
Use the semi-normal vector formula (Haversine) to calculate the distance between two points based on the latitude and longitude:


  

Then, you can use uasort or array_multisort to list users from near to far. for example, the users named win, osx, and lin are listed as follows:

 Array ('dis '=> 1024, 'age' => 31), 'osx' => array ('dis' => 512, 'age' => 15 ), 'line' => array ('dis '=> 512, 'age' => 25); foreach ($ arr as $ k => $ v) {$ sort ['dis '] [$ k] = $ v ['dis']; $ sort ['age'] [$ k] = $ v ['age'];} // sort by distance in ascending order. if the distance is the same, array_multisort ($ sort ['dis '], SORT_ASC, $ sort ['age'], SORT_DESC, $ arr); echo json_encode ($ arr ); // {"lin": {"dis": 512, "age": 25}, "osx": {"dis": 512, "age": 15 }, "win": {"dis": 1024, "age": 31 }}

Interfaces provided by Baidu

GeoIP https://sjolzy.cn/GeoIP-PHP-v...

Share a favorite function with the landlord.Directly call
Https://gist.github.com/wujun...

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.