zoukankan      html  css  js  c++  java
  • php百度地图根据地址、ip获取经纬度,计算驾车距离

    function get_lat_and_lng_ByIP($ip)
    {
    $content = file_get_contents("https://api.map.baidu.com/location/ip?ak=百度AK&ip=$ip&coor=gcj02");
    $json = json_decode($content,true);

    $lng=$json['content']['point']['x'];//提取经度数据

    $lat=$json['content']['point']['y'];//提取纬度数据

    return array('lat'=>$lat,'lng'=>$lng);
    }

    function get_lat_and_lng_Byaddress($address,$city)
    {
    $content = file_get_contents("https://api.map.baidu.com/geocoding/v3/?address=$address&output=json&ak=百度AK&city=$city&ret_coordtype=gcj02");
    $json = json_decode($content,true);

    $lng=$json['result']['location']['lng'];//提取经度数据

    $lat=$json['result']['location']['lat'];//提取纬度数据

    return array('lat'=>$lat,'lng'=>$lng);
    }
    if(!empty($_SESSION['lat'])&&!empty($_SESSION['lng'])){
    $lat = $_SESSION['lat'];
    $lng = $_SESSION['lng'];
    }elseif(!empty($_SESSION['mydingwei'])){
    $address=$_SESSION['mydingwei'];
    $city=$_SESSION['city_name'];
    $zuobiao=$this->get_lat_and_lng_Byaddress($address,$city);
    $lat = $zuobiao['lat'];
    $lng = $zuobiao['lng'];
    }else{
    $user_ip= $_SERVER["REMOTE_ADDR"];//获取客户端IP
    $zuobiao=$this->get_lat_and_lng_ByIP($user_ip);
    $lat = $zuobiao['lat'];
    $lng = $zuobiao['lng'];
    }

    //计算驾车距离
    $distance = m('util')->GetDistance($lat, $lng, $lat2, $lng2, 2);
    function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
    $content = file_get_contents("https://api.map.baidu.com/directionlite/v1/driving?origin=$lat1,$lng1&destination=$lat2,$lng2&ak=百度AK&coord_type=gcj02");
    $json = json_decode($content,true);
    $s=$json['result']['routes'][0]['distance'];
    if( 1 < $len_type )
    {
    $s /= 1000;
    }
    return round($s, $decimal);
    }
  • 相关阅读:
    python numpty 中shape的用法
    卷积神经网络中参数的计算
    linux学习
    数据结构排序算法总结
    剑指offer 刷题 01
    30-大道至简——随机森林如何将分类器由弱变强
    29-用python构造一棵决策树
    28-决策树算法——简单有效的概率模型
    27-如何度量分类算法的性能好坏(Scoring metrics for classification)
    26-史上最简单的分类算法——KNN
  • 原文地址:https://www.cnblogs.com/yangchong/p/15055804.html
Copyright © 2011-2022 走看看