<?php class GeoHelper { /** * @param int $lat1 * @param int $lon1 * @param int $lat2 * @param int $lon2 * @param string $unit * @return */ public static function distance($lat1, $lon1, $lat2, $lon2, $unit = "K") { $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad ($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $unit = strtoupper($unit); if ($unit == "K") { return ($miles * 1.609344); } else if ($unit == "N") { return ($miles * 0.8684); } else { //mi return $miles; } } /** * * @param string $address * @param string $apikey * @return array [1]:lat [0]:lng */ public static function getLatLng($address, $apikey) { $find = array("\n", "\r", " "); $replace = array("", "", "+"); $address = str_replace($find, $replace, $address); $url = 'http://maps.google.com/maps/geo?q=' . $address . '&key=' . $apikey . '&sensor=false&output=xml&oe=utf8'; $response = self::xml2array($url); $coordinates = $response['kml']['Response']['Placemark']['Point']['coordinates']; if (!empty($coordinates)) { $point_array = split(",", $coordinates); return $point_array; } } }
Calculate the distance between two points on the map, using Google Maps
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
![How to Calculate the Distance Between Two Points on Google Maps V3?](https://img.php.cn/upload/article/001/246/273/173264689284735.jpg)
27 Nov 2024
Calculating Distance Between Points in Google Maps V3In Google Maps V3, the distance between two markers can be calculated using the Haversine...
![How to Calculate Distance Between Two Points on Earth Using Latitude and Longitude?](https://img.php.cn/upload/article/001/246/273/173039690918427.jpg)
01 Nov 2024
Introducing Latitude and Longitude-Based Distance CalculationsCalculating the distance between two points on the Earth's surface using their...
![How to Calculate the Distance Between Two Points on Earth Using Latitude and Longitude?](https://img.php.cn/upload/article/001/246/273/173035314366161.jpg)
31 Oct 2024
Latitude and Longitude: Calculating Distance Between Two PointsCalculating the distance between two points on the globe requires knowledge of...
![How Do I Link Static Libraries That Depend on Other Static Libraries?](https://img.php.cn/upload/article/001/246/273/173408941480807.jpg)
13 Dec 2024
Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...
![How to Silence TensorFlow\'s Debugging Output?](https://img.php.cn/upload/article/001/246/273/173013069196898.jpg)
28 Oct 2024
Suppression of Tensorflow Debugging OutputTensorflow prints extensive information about loaded libraries, found devices, and other debugging data...
![How Does jQuery Simplify DOM Manipulation for Web Developers?](https://img.php.cn/upload/article/001/246/273/173584251998599.jpg)
03 Jan 2025
Overflow: Hidden and Expansion of HeightjQuery distinguishes itself from other JavaScript libraries through its cross-platform compatibility and...
![](/static/imghw/down_right.png)
![](/static/imghw/taglogo.png)
Hot Tools
![PHP library for dependency injection containers](https://img.php.cn/upload/manual/000/000/001/5e2171bf3c005481.png)
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images
![](/static/imghw/taglogo.png)