Skip to content
Advertisement

Calculate the distance between 2 position Google maps Api

We want to create a Line Bus of our City , what kind of Maps Api , we can use , My friend proposed to use Google Map Api. We want to know howto get the distance between 2 places in meter , i found the method distanceFrom() and it generate the distance in the air and not between street . We have a Table of Address , and we want to compare it with a position who came from the user to find the Closest address. so we need to find the lower distance between the position and all the list of address in the table.

Advertisement

Answer

My code is get distance of 2 point or 2 location in map with google map deriction service

    var PolylineOption = {
        strokeColor : "blue",
        strokeOpacity : 0.5,
        strokeWeight : 5
    };
    var request = {
        origin : polyline[0].start, // điểm đầu
        waypoints : polyline[1].waypoint,
        destination : polyline[2].end, // Điểm cuối
        optimizeWaypoints : true,
        travelMode : google.maps.TravelMode.DRIVING
    };
    rendererOptions = {
                draggable : true,
                suppressMarkers : true,
                polylineOptions : this.PolylineOption
    };
    var Service = new google.maps.DirectionsService();
    var Display = new google.maps.DirectionsRenderer(rendererOptions);
    Display.setMap(map);
    Service.route(this.request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            distance += response.routes[0].legs[0].distance.value;
            // distance of 2 location
        }
    });
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement