Permalink
Browse files

Update route to return some information about the calculated route

  • Loading branch information...
mmarcon committed Nov 23, 2012
1 parent 9b0bd91 commit 32594b36d1ada806c1c90d571171626eb949de08
Showing with 54 additions and 4 deletions.
  1. +1 −1 dist/extensions/route.min.js
  2. +26 −1 src/examples/index.route.html
  3. +27 −2 src/extensions/route.js

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -13,22 +13,47 @@
right: 0;
background: white;
}
.route {
position: absolute;
bottom: 0;
right: 0;
left: 0;
height: 100px;
background: rgba(255,255,255,0.7);
}
.route p {
font-family: 'Helvetica', sans-serif;
padding: 6px 3px;
margin: 0;
line-height: 1;
font-size: 28px;
font-weight: 100;
text-align: right;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="route"></div>
<!-- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/zepto/1.0rc1/zepto.min.js"></script>
<script type="text/javascript" src="../zepto.adapter.js"></script>
<script type="text/javascript" src="../jhere.js"></script>
<script type="text/javascript" src="../extensions/route.js"></script>
<script type="text/javascript">
function onroute(route){
$('.route').append('<p>Time: ' + Math.floor(route.time/60) + ' min</p>')
.append('<p>Length: ' + Math.floor(route.length/1000) + ' Km</p>')
}
$(window).on('load', function(){
$('#map').jHERE({
center: [52.5, 13.3],
zoom: 10
}).jHERE('route', [52.711, 13.011], [52.514, 13.453]);
})
.jHERE('route', [52.711, 13.011], [52.514, 13.453], {onroute: onroute});
});
</script>
</body>
View
@@ -37,9 +37,13 @@
// //avoidDirtRoad, avoidPark, preferHOVLane, avoidStairs
// trafficMode: 'default', //can be enabled, disabled, default
// width: 4, //width in px of the route drawn on the map
// color: '#ff6347' //color of the route drawn on the map
// color: '#ff6347', //color of the route drawn on the map,
// onroute: function(route){} //optional callbacks that gets the list of maneuvers with some
// //basic info, plus total time (seconds) and length (meters)
//}</code></pre>
//
//Once route is calculated a jhere.route event is also triggered.
//
//`marker` is an object containing the same options used for
//`$('.selector').jHERE('marker')`. Options apply to both start and destionation markers.
route = function(from, to, options){
@@ -51,7 +55,7 @@
/*Call me with the correct context!*/
done = function(router, key, status) {
var routes, routeContainer, poly, r;
var routes, routeContainer, poly, r, leg, info = {}, evt;
if (status == 'finished') {
routes = router.getRoutes();
r = routes[0];
@@ -74,6 +78,27 @@
this.marker(w.originalPosition, o);
}, this));
this.map.objects.add(routeContainer);
/*Now let's look into the route infos*/
leg = r.legs && r.legs.length && r.legs[0];
info.time = leg.travelTime;
info.length = leg.length;
info.maneuvers = $.map(leg.maneuvers, function(m){
return {

This comment has been minimized.

Show comment
Hide comment
@axeff

axeff Feb 28, 2014

why only these three informations and not the whole object?
As well as it returns undefined for streetName and routeName.

@axeff

axeff Feb 28, 2014

why only these three informations and not the whole object?
As well as it returns undefined for streetName and routeName.

This comment has been minimized.

Show comment
Hide comment
@mmarcon

mmarcon Feb 28, 2014

Owner

I guess when I implemented this I thought I'd keep it easier. I could certainly expose everything that is in the object.

Regarding undefined for streetName and routeName I'd need to check. I am a bit busy until end of next week though. Would you like to take a look and perhaps submit a pull request?

@mmarcon

mmarcon Feb 28, 2014

Owner

I guess when I implemented this I thought I'd keep it easier. I could certainly expose everything that is in the object.

Regarding undefined for streetName and routeName I'd need to check. I am a bit busy until end of next week though. Would you like to take a look and perhaps submit a pull request?

street: m.streetName,
length: m.length,
route: m.routeName
};
});
/*Fire callback if present*/
if(typeof options.onroute === 'function') {
options.onroute.call(this.element, info);
}
/*And trigger event*/
evt = $.Event('jhere.route', {
route: info,
target: this.element
});
$(this.element).trigger(evt);
} else if (status == 'failed') {
$.error('Failed to calcolate route');
}

0 comments on commit 32594b3

Please sign in to comment.