百度地图能帮我计算一下从北京到上海的直线距离是多少呢?
- 内容介绍
- 文章标签
- 相关推荐
本文共计501个文字,预计阅读时间需要3分钟。
请访问百度地图官网,申请您的ak。然后,将以下代码复制到您的页面中:
请自行到百度地图官网申请您的ak$result->result[0]->location->lat, "lng" => $result->result[0]->location->lng ); for ($i=0; $i<$length; $i++){ $url = urldecode("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; $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 = "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 = "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);
本文共计501个文字,预计阅读时间需要3分钟。
请访问百度地图官网,申请您的ak。然后,将以下代码复制到您的页面中:
请自行到百度地图官网申请您的ak$result->result[0]->location->lat, "lng" => $result->result[0]->location->lng ); for ($i=0; $i<$length; $i++){ $url = urldecode("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; $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 = "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 = "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);

