在谷歌地图中查找路线之间的纬度和经度

时间:2014-08-04 05:41:35

标签: javascript json google-maps

我在这个问题上遇到了很多。我有一套纬度,经度,价格细节作为json。我正在使用Google地图DirectionsService。我需要找到超过给定路线的纬度和经度。我用json细节绘制标记。我需要找到路线上的纬度和经度。有了这个结果,我需要找到它的价格。我使用以下代码。对不起我的英语不好。提前致谢

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<div class = 'map_div'>     
    <div id="map-canvas" style="float:left;width:100%;"></div>
</div>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
    var toll_details = '[{"id":"1","latitude":"-23.413133333","longitude":"-46.36105","price":"4.2"},{"id":"2","latitude":"-23.413133333","longitude":"-46.36105","price":"4.2"},{"id":"3","latitude":"-22.930330555","longitude":"-45.360716666","price":"10.1"},{"id":"4","latitude":"-22.495041666","longitude":"-44.569480555","price":"10.1"},{"id":"5","latitude":"-22.495041666","longitude":"-44.569480555","price":"10.1"},{"id":"6","latitude":"-22.716169444","longitude":"-43.716736111","price":"10.1"},{"id":"7","latitude":"-22.716169444","longitude":"-43.716736111","price":"10.1"},{"id":"8","latitude":"-22.716169444","longitude":"-43.716736111","price":"10.1"},{"id":"9","latitude":"-22.716169444","longitude":"-43.716736111","price":"10.1"},{"id":"10","latitude":"-22.930330555","longitude":"-45.360716666","price":"10.1"},{"id":"11","latitude":"-23.338847222","longitude":"-46.150333333","price":"4.5"},{"id":"30","latitude":"-21.547725","longitude":"-45.241530555","price":"1.5"},{"id":"31","latitude":"-21.000197222","longitude":"-44.966825","price":"1.5"},{"id":"32","latitude":"-20.591744444","longitude":"-44.701375","price":"1.5"},{"id":"33","latitude":"-20.268288888","longitude":"-44.423925","price":"1.5"},{"id":"34","latitude":"-23.322011111","longitude":"-46.581397222","price":"1.5"},{"id":"35","latitude":"-22.907088888","longitude":"-46.423438888","price":"1.5"},{"id":"511","latitude":"-20.830697222","longitude":"-51.504008333","price":"4.2"}]';
    var all_tolls = JSON.parse(toll_details);

    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map, response, marker;
    var options = {
        types: ['(cities)'],
        componentRestrictions: {country: 'br'}
    };


    function initialize() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var mapOptions = {
            zoom: 6,
            center: new google.maps.LatLng(-23.5500, -46.6333)
        }
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        calcRoute();

        directionsDisplay.setMap(map);
    }

    function finding(all_tolls) {
        for (i = 0; i < all_tolls.length; i++) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(all_tolls[i].latitude, all_tolls[i].longitude),
                map: map
            });
        }
    }

    function calcRoute() {
        var start = 'São Paulo, Brazil';
        var end = 'Rio de Janeiro, Brazil';

        var waypts = [];

        var request = {
            origin: start,
            destination: end,
            waypoints: waypts,
            optimizeWaypoints: true,
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.METRIC
        };
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);

            }
        });
        finding(all_tolls)
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
    html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
    }
    .map_div {
        border: 1px solid #CCCCCC;
        border-radius: 4px;
        float: left;
        height: 500px;
        margin-left: 4px;
        margin-top: 10px;
        padding: 10px;
        width: 700px;
    }
</style>

1 个答案:

答案 0 :(得分:2)

根据返回的路径创建折线(例如,在overview_path上),然后迭代all_tolls并使用google.maps.geometry.poly.isLocationOnEdge检查给定LatLng是否放置在(或附近)折线。