php百度地图计算两地现实距离
请自行到百度地图官网申请您的ak
<!--前端获取手机经纬度的代码-->
<!--<!DOCTYPE html>-->
<!--<html lang="en">-->
<!---->
<!--<head>-->
<!-- <meta charset="UTF-8">-->
<!-- <title>text</title>-->
<!-- <script>-->
<!-- function getLocation() {-->
<!-- if (navigator.geolocation) {-->
<!-- navigator.geolocation.getCurrentPosition(showMap,errorMap);-->
<!-- } else {-->
<!-- alert("您的浏览器不支持")-->
<!-- }-->
<!---->
<!-- }-->
<!-- function showMap(position){-->
<!-- var longitude = position.coords.longitude;-->
<!-- var latitude = position.coords.latitude;-->
<!-- alert("您的位置是:"+longitude+ "," + latitude);-->
<!-- }-->
<!-- function errorMap(value){-->
<!-- switch(value.code){-->
<!-- case 1: alert("位置请求被拒绝");break;-->
<!-- case 2: alert("暂时获取不到位置信息");break;-->
<!-- case 3: alert("请求超时");break;-->
<!-- case 4: alert("未知错误");break;-->
<!-- }-->
<!-- }-->
<!-- -->
<!-- getLocation();-->
<!-- </script>-->
<!-- -->
<!--</head>-->
<!--</html>-->
<?php
/**
* 计算两个地址之间的距离,单位:米
* 地址要尽量详细,减小误差
* @param address
* @param $param
* @return $param
*/
function calculateDistance($address,$param){
if (!is_array($param)){
throw new Exception("参数必须为array");
}
$length = count($param);
$url = urldecode("http://api.map.baidu.com/cloudgc/v1?ak=您的ak&address=".$address);
$result = json_decode(get($url));
$startLatLng = array(
"lat" => $result->result[0]->location->lat,
"lng" => $result->result[0]->location->lng
);
for ($i=0; $i<$length; $i++){
$url = urldecode("http://api.map.baidu.com/cloudgc/v1?ak=您的ak&address=".$param[$i]['address']);
$result = json_decode(get($url));
$endLatLng = array(
"lat" => $result->result[0]->location->lat,
"lng" => $result->result[0]->location->lng
);
$distance = array( "distance"=>getDistance($startLatLng,$endLatLng),"unit"=>"m");
array_push($param[$i],$distance);
}
return $param;
}
// $add = array(
// array("address"=>"福建省厦门市集美区"),
// array("address"=>"北京市海淀区上地十街10号")
// );
// print_r(calculateDistance("福建省厦门市集美区",$add)); /**
* 发送GET请求
* @param url
* @param param
* @return result
*/
function get($url, $param=array()){
if(!is_array($param)){
throw new Exception("参数必须为array");
}
$p='';
foreach($param as $key => $value){
$p=$p.$key.'='.$value.'&';
}
if(preg_match('/\?[\d\D]+/',$url)){//matched ?c
$p='&'.$p;
}else if(preg_match('/\?$/',$url)){//matched ?$
$p=$p;
}else{
$p='?'.$p;
}
$p=preg_replace('/&$/','',$p);
$url=$url.$p;
//echo $url;
$http =curl_init($url);
curl_setopt($http, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($http, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($http,CURLOPT_RETURNTRANSFER,1);
curl_setopt($http, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($http, CURLOPT_RETURNTRANSFER,1);
curl_setopt($http, CURLOPT_HEADER,false);
$result=curl_exec($http);
curl_close($http);
return $result;
}
/**
* 计算两点之间距离
* @param start
* @param end
* @return 米
*/
function getDistance($startLatLng,$endLatLng){
if (is_array($startLatLng)&&is_array($endLatLng)){
$lat1 = (pi()/180)*$startLatLng['lat'];
$lat2 = (pi()/180)*$endLatLng['lat'];
$lng1 = (pi()/180)*$startLatLng['lng'];
$lng2 = (pi()/180)*$endLatLng['lng'];
//地球半径
$R = 6371;
//两点间的距离 km,如果想要米的话,结果*1000就可以了
$d = acos(sin($lat1)*sin($lat2)+cos($lat1)*cos($lat2)*cos($lng2-$lng1))*$R;
}else{
$d = 1;
}
return $d*1000;
}
/**
* 通过ip获取地址
* @param $ip
* @throws Exception
* @return $result
*/
function getAddressByIp($ip){
if (!filter_var($ip,FILTER_VALIDATE_IP)){
throw new Exception("IP无效");
}
$url = "http://api.map.baidu.com/location/ip?ak=您的ak&coor=bd09ll&ip=".$ip;
$result = json_decode(get($url));
if ($result->status != 0){
throw new Exception("获取坐标失败");
}
$url = "http://api.map.baidu.com/geocoder/v2/?location=".$result->content->point->y.",".$result->content->point->x."&ak=您的ak&output=json";
$result= json_decode(get($url));
echo $result->result->formatted_address;
return $result->result->formatted_address;
} //getAddressByIp("120.41.85.225");/**
* 通过GPS定位拿到具体位置,电脑只有IE、edge才有定位权限
* @param $latLng=array('lat'=>?,'lng'=>?)
* @return $result->result->formatted_address
*/
function getAddressByGPS($latLng)
{
$url = "http://api.map.baidu.com/geocoder/v2/?location=" .$latLng['lat'].",".$latLng['lng']. "&ak=您的ak&output=json";
$result = json_decode(get($url));
return $result->result->formatted_address;
}
$latLng = array(
'lat'=>24.609098,
'lng'=>118.045559
);
echo getAddressByGPS($latLng);
php百度地图计算两地现实距离的更多相关文章
- iOS 百度地图计算两个点时间的距离
最近在解项目bug,更新地位城市的时候有个错误,后来想在位置改变多少距离之后,再取更新位置,这个功能去年做过.但是又忘记了! 所以还是记录一下吧. 百度地图提供了一个方法: BMKMapPointFo ...
- JS高德地图计算两地之间的实际距离
这个是通过导航的方式来获取两地之间的实际距离,和消耗的时间(key值自己去申请哈) <!doctype html> <html> <head> <meta c ...
- 百度地图坐标之间的距离php
function GetDistance($lat1, $lng1, $lat2, $lng2){ define('PI',3.1415926535898); define('EARTH_RADIUS ...
- 百度地图 - demo
项目需要集成百度地图,那么关于如何集成百度地图的事,就自己去百度开放平台查看文档吧,这是非常简单的事,在这里就不多说了. 那么下面我就说说我在这个demo里所做的事. 首先,项目需要具备定位及计算两地 ...
- Java根据两点的经纬度来计算之间的距离
import java.util.HashMap; import java.util.Map; public class MapDistance { private static double EAR ...
- 百度地图api 实例 自动提示 并计算两地的行驶距离
百度地图api 实例 自动提示 并计算两地的行驶距离 <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- 百度地图Api 根据两个坐标点计算距离
百度地图Android Sdk的Api里面,没有现成的直接获取两个坐标点之间距离的方法,但是,在jsapi里面,有直接计算距离的方法. class Point: pass def max(a,b): ...
- 【百度地图API】如何根据摩卡托坐标进行POI查询,和计算两点距离
原文:[百度地图API]如何根据摩卡托坐标进行POI查询,和计算两点距离 摘要: 百度地图API有两种坐标系,一种是百度经纬度,一种是摩卡托坐标系.在本章你将学会: 1.如何相互转换这两种坐标: 2. ...
- Java-根据经纬度计算距离(百度地图距离)
最近碰到一个需求,需要根据两个点的经纬度查询两点的距离.感觉以后还会用到,所以小记一波. 第一步:添加Maven依赖. <dependency> <groupId>org.ga ...
随机推荐
- 洛谷P1291 [SHOI2002]百事世界杯之旅
题目链接: kma 题目分析: 收集邮票的弱弱弱弱化版,因为是期望,考虑倒推 设\(f[i]\)表示现在已经买齐了\(i\)种,距离买完它的剩余期望次数 那么下一次抽有\(\frac{i}{n}\)的 ...
- Glassfish安装、基本使用、在idea中配置Glassfish
Glassfish安装.基本使用. 一.glassfish简介 glassfish是一款web应用服务器,和tomcat一样,也是一款优秀的Servlet容器. 二.glassfish知识点 1.do ...
- 由VMnet引起的browser-sync故障解决方案
(2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2016年7月11日) 今天晚上,前端组的小伙伴问我说能不能帮忙看看他的电脑为什么在安装了browser-sync插件以后, ...
- Pandas系列-读取csv/txt/excel/mysql数据
本代码演示: pandas读取纯文本文件 读取csv文件 读取txt文件 pandas读取xlsx格式excel文件 pandas读取mysql数据表 import pandas as pd 1.读取 ...
- <scrapy爬虫>爬取腾讯社招信息
1.创建scrapy项目 dos窗口输入: scrapy startproject tencent cd tencent 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # - ...
- wpf textbox只能输入数字,屏蔽中文输入
1.设置textbox属性InputMethod.IsInputMethodEnabled="False" 2.增加KeyDown事件 private void TextBox_K ...
- java笔试之取近似值
写出一个程序,接受一个正浮点数值,输出该数值的近似整数值.如果小数点后数值大于等于5,向上取整:小于5,则向下取整. package test; import java.util.*; import ...
- Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...
- 【ZJOI2007】【BZOJ1059】矩阵游戏 匈牙利算法
题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换 ...
- new运算符与malloc函数(还需要修改)
细说new与malloc的10点区别 C++ 自由存储区是否等价于堆? 浅谈new/delete和malloc/free的用法与区别 new和malloc都是在对上开辟内存,但尽量使用new. 使用m ...