Search code examples
androidiosiphonegoogle-mapstitanium

Draw rout in titanium Map


this is my code:

var win = Ti.UI.createView({
    left : 0,
    top : 0,
    right : 0,
    top : 0
});
Ti.UI.currentWindow.add(win);

var mapview = Titanium.Map.createView({
    mapType : Titanium.Map.STANDARD_TYPE,
    region : {
        latitude : 0,
        longitude : 0,
        latitudeDelta : 1,
        longitudeDelta : 1
    },
    animate : true,
    regionFit : true,
    userLocation : false,
});
win.add(mapview);

currenposition();

function drawrout(longitude, latitude) {

    Titanium.Geolocation.getCurrentPosition(function(e) {
        if (e.error) {
            alert('HFL cannot get your current location');
            return;
        } else {
            var xhr = Titanium.Network.createHTTPClient();
            xhr.open('GET', "http://maps.googleapis.com/maps/api/directions/json?origin=" + latitude + ',' + longitude + "&destination=" + (latitude - 0.01) + ',' + (longitude - 0.01) + "&sensor=true");
            xhr.onload = function() {
                var xml = this.responseText;
                var points = [];

                // Bellow Variable have the step of the current location to destination  Location. Using the Steps we going to create a route.

                var position = JSON.parse(this.responseText).routes[0].legs[0].steps;
                if (position[0] != null) {

                    points.push({
                        latitude : position[0].start_location.lat,
                        longitude : position[0].start_location.lng,
                    });

                    // Here we use the for loop to collect all the steps and push it to the array and use this array to form the route in android.

                    for (var i = 0; i < position.length; i++) {

                        points.push({
                            latitude : position[i].end_location.lat,
                            longitude : position[i].end_location.lng,
                        });
                    }
                } else {
                    alert('no route');
                }

                var route = {
                    name : "india",
                    points : points,
                    color : "red",
                    width : 5
                };

                mapview.addRoute(route);
            };
            xhr.send();
        }
    });
}

function currenposition() {

    Titanium.Geolocation.getCurrentPosition(function(e) {
        if (e.error) {
            alert('HFL cannot get your current location');
            return;
        } else {
            var longitude = e.coords.longitude;
            var latitude = e.coords.latitude;
            mapview.region = {
                latitude : latitude,
                longitude : longitude,
                latitudeDelta : 0.01,
                longitudeDelta : 0.01
            };

            drawrout(longitude, latitude);
        }
    });
}

white this code I get sum line between two points but the rout is not god defined this is the complete rout

detailed image

like we can se it on the images the rout is defined by some points and a line between them. how can i get all points of the street so a can draw the line on top of the road?


Solution

  • I solved my problem following this link:

    https://archive.appcelerator.com/question/141002/plot-a-route-on-google-map-in-android#answer-245304