JavaScript-How do I get the latitude and longitude of a user and calculate the distance between two locations?

Source: Internet
Author: User
Tags geoip
How do I get the latitude and longitude of my current position? Once you know the latitude and longitude of two locations, how do you calculate the distance between the two locations?
Recently doing a public number, need to get the latitude and longitude of the user, and then calculate the user's latitude and longitude and the distance from another location? How do you achieve this? Please help the great god, thank you!

Reply content:

How do I get the latitude and longitude of my current position? Once you know the latitude and longitude of two locations, how do you calculate the distance between the two locations?
Recently doing a public number, need to get the latitude and longitude of the user, and then calculate the user's latitude and longitude and the distance from another location? How do you achieve this? Please help the great god, thank you!

PHP implementation of "Nearby people" function, according to IP to determine latitude and longitude, based on latitude and longitude to calculate distance

PHP installation GeoIP extensions and databases get information about the country/city/latitude and longitude of your visitors based on IP
You can then use geoip_record_by_name($_SERVER['REMOTE_ADDR']) the user IP to determine the latitude and longitude.
Note: Geoip_record_by_name () returns a negative number for the West and south latitudes.

5000 meters to warp latitude:
Latitude latitude:1 deg = 110852 m
Longitude longitude:1 deg = 111320*cos (LAT) m
On the same meridian, a latitude of about 110852 meters
On the same parallel, the difference is about 111320*cos (LAT) meters (lat is the latitude of that latitude)


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

The database user table has two fields, each of which stores the user's longitude lat and latitude lon.
($lat-$y) <= lat <= ($lat + $y)
($lon-$x) <= Lon <= ($lon + $x)
This range is a rough range, the following calculation distance after the users more than 5 km removed.

Based on the latitude and longitude of the users queried above,
Use the half-normal vector formula (haversine) to calculate the distance between two points based on latitude and longitude:


  
   

然后就可以用uasort或array_multisort由近到远列出用户了,比如有名为win,osx,lin这3个用户:

  array( 'dis' => 1024, 'age' => 31 ), 'osx' => array( 'dis' => 512, 'age' => 15 ), 'lin' => array( 'dis' => 512, 'age' => 25 ));foreach($arr as $k => $v) { $sort['dis'][$k] = $v['dis']; $sort['age'][$k] = $v['age'];}//先按距离升序排序,如果距离相同,则按年龄降序排序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}}

Baidu provides interface

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

to the landlord to share a function of my favorite bar call directly
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.