Skip to content

Commit 38294cd

Browse files
committed
Merge pull request #35 from thingsinjars/master
Add method to clear routes in route extension
2 parents a93fbad + d5ff7c3 commit 38294cd

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

dist/extensions/route.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extensions/route.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2020
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2121
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
23-
;(function($){
24-
var _ns, route, _default = {
23+
;
24+
(function($) {
25+
var _ns, clearRoutes, routeContainer, route, _default = {
2526
type: 'shortest',
2627
transportMode: 'car',
2728
options: '',
@@ -35,8 +36,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3536
zoomTo: true
3637
};
3738

38-
function normalize(position){
39-
return position instanceof Array ? {latitude: position[0], longitude: position[1]} : position;
39+
function normalize(position) {
40+
return position instanceof Array ? { latitude: position[0], longitude: position[1] } : position;
4041
}
4142

4243
//### Calculate the route between 2 points
@@ -69,14 +70,14 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6970
//
7071
//`marker` is an object containing the same options used for
7172
//`$('.selector').jHERE('marker')`. Options apply to both start and destionation markers.
72-
route = function(from, to, via, options){
73+
route = function(from, to, via, options) {
7374
var router, wp, done, cleanOptions;
7475
_ns = _ns || nokia.maps;
7576
from = normalize(from);
7677
to = normalize(to);
7778

7879
//in this case options is the options object, like expected
79-
if( via instanceof Array ){
80+
if (via instanceof Array) {
8081
options = $.extend({}, _default, options);
8182
}
8283
//in this case via is the options object, probably older implementation of plugin
@@ -86,7 +87,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8687
}
8788
/*Call me with the correct context!*/
8889
done = function(router, key, status) {
89-
var routes, routeContainer, poly, r, info = {}, evt;
90+
var routes, poly, r, info = {}, evt;
9091
if (status === 'finished') {
9192
routes = router.getRoutes();
9293
r = routes[0];
@@ -98,19 +99,21 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9899
strokeColor: options.color
99100
})
100101
});
101-
routeContainer = new _ns.map.Container();
102+
if (!routeContainer) {
103+
routeContainer = new _ns.map.Container();
104+
this.map.objects.add(routeContainer);
105+
}
102106
routeContainer.objects.add(poly);
103107
/*And add the markers next. For markers we use the corresponding jHERE method for now.*/
104-
$.each(r.waypoints, $.proxy(function(i, w){
108+
$.each(r.waypoints, $.proxy(function(i, w) {
105109
var o = $.extend({}, options.marker);
106-
if(options.marker.text === '#') {
110+
if (options.marker.text === '#') {
107111
o.text = i + 1;
108112
}
109113
this.marker(w.originalPosition, o);
110114
}, this));
111-
this.map.objects.add(routeContainer);
112115
/*Zoom map to bounds of route*/
113-
if(options.zoomTo){
116+
if (options.zoomTo) {
114117
this.map.zoomTo(routeContainer.getBoundingBox(), false, "default");
115118
}
116119
/*Now let's look into the route infos*/
@@ -120,7 +123,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
120123
info.legs = r.legs;
121124

122125
/*Fire callback if present*/
123-
if(typeof options.onroute === 'function') {
126+
if (typeof options.onroute === 'function') {
124127
options.onroute.call(this.element, info);
125128
}
126129
/*And trigger event (jQuery only)*/
@@ -139,7 +142,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
139142

140143
wp = new _ns.routing.WaypointParameterList();
141144
wp.addCoordinate(from);
142-
$.each(via, function(i,item){
145+
$.each(via, function(i, item) {
143146
wp.addCoordinate(normalize(item));
144147
});
145148
wp.addCoordinate(to);
@@ -155,5 +158,13 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
155158
router.calculateRoute(wp, [cleanOptions]);
156159
};
157160

161+
//###Clear all routes from the map
162+
clearRoutes = function() {
163+
if (routeContainer && routeContainer.objects) {
164+
routeContainer.objects.clear();
165+
}
166+
};
167+
158168
$.jHERE.extend('route', route);
169+
$.jHERE.extend('clearRoutes', clearRoutes);
159170
}(jQuery));

0 commit comments

Comments
 (0)