谷歌地图给出的距离与实际距离不同

时间:2014-07-14 18:11:08

标签: php google-maps

您好我正在尝试将谷歌地图集成到我的应用程序中但是从最近4天开始我只坚持当我输入长度和纬度时实际给出不同的结果并且我的脚本给出不同的距离。

我在这里展示我的代码,有人可以帮我解决我的自定义功能吗?

 function distance(lat1, lon1, lat2, lon2, unit) {
        var radlat1 = Math.PI * lat1/180
        var radlat2 = Math.PI * lat2/180
        var radlon1 = Math.PI * lon1/180
        var radlon2 = Math.PI * lon2/180
        var theta = lon1-lon2
        var radtheta = Math.PI * theta/180
        var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
        dist = Math.acos(dist)
        dist = dist * 180/Math.PI
        dist = dist * 60 * 1.1515
        if (unit=="K") { dist = dist * 1.609344 }
        if (unit=="N") { dist = dist * 0.8684 }
        return dist;
    }



function find_nearest()
    {

        var max_dis = 791364782684;
        get_location();
        for ( var a in data['data'] )   
        {

            data['data'][a].distance = Math.sqrt (Math.pow(data['data'][a].latitude - lat,2) + Math.pow(data['data'][a].longitude - long,2) );

            if ( data['data'][a].distance < max_dis)
            {
                max_dis = data['data'][a].distance;
                data['avg_lat'] = data['data'][a].latitude;
                data['avg_lon'] = data['data'][a].longitude;
                data['nearest_key']=a;
            }
        }
        data['zoom']=15;


 function showDirection()
    {
        $("#errLbl").html('').hide();
        //                        alert($("#selected_postal").val())
        directionsDisplay.setMap(null);

        directionsService = new google.maps.DirectionsService();
        directionsDisplay = new google.maps.DirectionsRenderer();

        directionsDisplay.setOptions({'suppressInfoWindows':true});   //to disable popup on google pin
        directionsDisplay.setMap(map);
        //alert(document.getElementById('selected_panel').value)
        if(document.getElementById('selected_panel').value=='1'){
            var request = {
                origin: document.getElementById('address_from').value, 
                destination: document.getElementById('selected_postal').value,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };
        }
        else if(document.getElementById('selected_panel').value=='2'){
            var request = {
                origin: document.getElementById('selected_postal').value, 
                destination: document.getElementById('address_to').value,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };
        }
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                $("#errLbl").html('').hide();
                directionsDisplay.setDirections(response);
            }
            else{
                $("#errLbl").html('Address is not recognized.</br>Please try again.');
            }
        });
        //            if($("#errLbl").html()!='')
        $("#errLbl").show();
    }

1 个答案:

答案 0 :(得分:2)

你正在计算&#34;当一只鸟飞过&#34;距离[红线],谷歌正在计算&#34;作为汽车驾驶&#34;距离[蓝线]。

没有一个公式可以应用于您的功能来重新创建Google正在做的事情,因为您的号码永远不会匹配。

as a bird flies vs. as a car drives