高德地图API javascript坐标间距离计算
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高德地图API javascript坐标间距离计算相关的知识,希望对你有一定的参考价值。
网上搜索了很久,貌似都没有能解答这问题的,跪求大神详细解答一下,万分感谢!
小弟希望计算出已知两坐标间直线距离,并在id为text的文本框中显示。
<script language="javascript" src="http://webapi.amap.com/maps?v=1.2&key=您申请的key值"></script>
<script language="javascript">
function a()
x1="123";
y1="456";
z1="(" + z1 + "," + y1 + ")" //z1为已知坐标1
x2="321";
y2="654";
z2="(" + z2 + "," + y2 + ")" //z2为已知坐标2
//求解这里应该怎么写
document.getElementById("text").value = XXX; //text显示距离值
</script>
<body>
<input type="text" id="text">
<input type="button" onclick="a()">
</body>
越详细越好啊大神们,小弟初学真心不懂。
<script language="javascript">
function a()
x1="123";
y1="456";
z1="(" + z1 + "," + y1 + ")" //z1已知坐标1
x2="321";
y2="654";
z2="(" + z2 + "," + y2 + ")" //z2已知坐标2
//求解里应该写
document.getElementById("text").value = XXX; //text显示距离值
</script>
<body>
<input type="text" id="text">
<input type="button" onclick="a()">
</body>
我来混经验
高德地图相关api使用计算两点地理坐标之间的距离
利用腾讯地图api&计算两点地理坐标之间的距离
$key = 'key参数'; //腾讯地图开发自己申请
$mode = 'driving'; //driving(驾车)、walking(步行)
$from = "39.14122,117.14428"; //
$to = "39.10149,117.10199"; //(格式:终点坐标;起点坐标):
$url = 'https://apis.map.qq.com/ws/distance/v1/?mode='.$mode.'&from='.$from.'&to='.$to.'&key='.$key;
$header = ['Content-Type:application/x-www-form-urlencoded'];
$options= [CURLOPT_HTTPHEADER => $header];
$ret = Http::get($url, '', $options);
$data = json_decode($ret,true);
按照相关地址返回经纬度及具体明细
$address = "北京南站";
$key = 'key参数'; //腾讯地图开发自己申请
$url="https://apis.map.qq.com/ws/geocoder/v1/?address=";
$ret = Http::get($url, ['address'=>$address,'key'=>$key], null);
$data = json_decode($ret,true);
CURL发送Request请求,含POST和REQUEST
/**
* 发送一个GET请求
* @param string $url 请求URL
* @param array $params 请求参数
* @param array $options 扩展参数
* @return mixed|string
*/
public static function get($url, $params = [], $options = [])
{
$req = self::sendRequest($url, $params, 'GET', $options);
return $req['ret'] ? $req['msg'] : '';
}
/**
* CURL发送Request请求,含POST和REQUEST
* @param string $url 请求的链接
* @param mixed $params 传递的参数
* @param string $method 请求的方法
* @param mixed $options CURL的参数
* @return array
*/
public static function sendRequest($url, $params = [], $method = 'POST', $options = [])
{
$method = strtoupper($method);
$protocol = substr($url, 0, 5);
$query_string = is_array($params) ? http_build_query($params) : $params;
$ch = curl_init();
$defaults = [];
if ('GET' == $method) {
$geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
$defaults[CURLOPT_URL] = $geturl;
} else {
$defaults[CURLOPT_URL] = $url;
if ($method == 'POST') {
$defaults[CURLOPT_POST] = 1;
} else {
$defaults[CURLOPT_CUSTOMREQUEST] = $method;
}
$defaults[CURLOPT_POSTFIELDS] = $params;
}
$defaults[CURLOPT_HEADER] = false;
$defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
$defaults[CURLOPT_FOLLOWLOCATION] = true;
$defaults[CURLOPT_RETURNTRANSFER] = true;
$defaults[CURLOPT_CONNECTTIMEOUT] = 120;
$defaults[CURLOPT_TIMEOUT] = 120;
// disable 100-continue
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
if ('https' == $protocol) {
$defaults[CURLOPT_SSL_VERIFYPEER] = false;
$defaults[CURLOPT_SSL_VERIFYHOST] = false;
}
curl_setopt_array($ch, (array)$options + $defaults);
$ret = curl_exec($ch);
$err = curl_error($ch);
if (false === $ret || !empty($err)) {
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return [
'ret' => false,
'errno' => $errno,
'msg' => $err,
'info' => $info,
];
}
curl_close($ch);
return [
'ret' => true,
'msg' => $ret,
];
}
以上是关于高德地图API javascript坐标间距离计算的主要内容,如果未能解决你的问题,请参考以下文章