Map route click event not working in Titanium Appcelerator

71 Views Asked by At

I'm trying to add a clickable route on Geocoder example from Appcelerator. The problem is that I'm not getting any event when clicking at the route object.

Here's my code:

var cord1= {
    latitude:29.078685,
    longitude:-110.971205,
};
var cord2= {
    latitude:29.081496,
    longitude:-110.959232,
};

var route1 = [cord1, cord2];
var route = MapModule.createRoute({
    points : route1,
    color : "red",
    width : 5.0
});

route.addEventListener('click', function(e){
    Ti.APP.info(e);
});

$.mapview.addRoute(route);
1

There are 1 best solutions below

0
Adam Paxton On

The Modules.Map.Route object doesn't have any events. None of the map objects do, except the map view itself, and we can use the mapview's click event to listen for clicks, and then check the clicksource property to see what was clicked on the map.

The catch is that routes won't generate a click event, but polylines do, so the workaround is to use a polyline and look for the clicksource in the mapview's click event. Something like this should work:

var coord1 = [-110.971205, 29.078685];
var coord2 = [-110.959232, 29.081496];

var route1 = [coord1, coord2];
var route = MapModule.createPolyline({
    points: route1,
    strokeColor: "#ff0000",
    strokeWidth: 5
});
$.mapview.addPolyline(route);

$.mapview.addEventListener('click', function (e) {
    //check the clicksource for 'polyline'
    console.log(e.clicksource);
});