hpneo / gmaps

the easiest way to use Google Maps
https://hpneo.dev/gmaps/
7.03k stars 1.28k forks source link

I need calculate and display distance for route #238

Closed ivan-borisov closed 10 years ago

ivan-borisov commented 11 years ago

I need calculate and display distance for route, this is mycode, i need add info - route distance in km. How calculate and display this info?

    $(document).ready(function() {
        var map;
        var dlat = destinationLat; ?>;
        var dlng = destinationLng; ?>;
        var olat = currentLat; ?>;
        var olng = currentLng; ?>;
        var mode = 'driving';

```
    function initRouting() {
         map.cleanRoute();
         map.drawRoute({
             origin: [olat, olng],
             destination: [dlat, dlng],
             travelMode: mode,
             strokeColor: '#FF4040',
             strokeOpacity: 0.6,
             strokeWeight: 6
         });
         map.fitZoom();
     }

    map = new GMaps({
        div: '#route_map',
        lat: olat,
        lng: olng,
        zoom: 13,
        streetViewControl: false
    });

    map.addMarker({
        lat: olat,
        lng: olng,
        title: 'You are here'
    });

    map.addMarker({
        lat: dlat,
        lng: dlng,
        title: 'Destination'
    });

    initRouting();

    $('#route_mode a').click(function(e) {
        switch($(this).attr('id')) {
            case 'walk':
                mode = 'walking';
                break;
            case 'bike':
                mode = 'bicycling';
                break;
            default:
                mode = 'driving';
        }
        initRouting();
        e.preventDefault();
    });

});
```

            
hpneo commented 11 years ago

You need to use getRoutes. Check the code of this example: http://hpneo.github.io/gmaps/examples/travel_route.html or read the documentation: http://hpneo.github.io/gmaps/documentation.html#GMaps-getRoutes