这是我的代码:
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);
}
});
}
就像我们可以在图像上看到溃败由某些点和它们之间的线定义。我如何才能获得街道上的所有点数,以便能够在路上画出线条?
答案 0 :(得分:1)
对于XML响应中的每个步骤,都有一个折线标记。 https://developers.google.com/maps/documentation/directions/intro#sample-response
您需要解码所有折线并添加点阵列中所有折线的所有点。 http://thesundarnataraj.blogspot.it/2012/11/decoding-polylines-from-google-maps.html
然后你可以使用这个数组来制作路线。
答案 1 :(得分:0)