我在地图上有两个点,我能够使用API进行测距,现在我需要在两个点之间画一条线,以便用户可以一路看到。我读到您需要使用折线,但不幸的是我不能。我将用户的GPS坐标作为点A-在地图上,在拖动事件中,我将获取点B的坐标。您可以在以下页面上看到一个示例:https://tojweb.tj/abb.php
你能帮忙吗?
我读到您需要使用折线,但不幸的是我不能。
if (navigator.geolocation) {
navigator.geolocation.getcurrentPosition(showPosition);
} else {
console.log("Geolocation is not supported by this browser.");
}
function showPosition(position) {
document.getElementById('mypos_lat').value=position.coords.latitude;
document.getElementById('mypos_lon').value=position.coords.longitude;
//alert("Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude);
}
var darection = new google.maps.DirectionsRenderer;
function initialize() {
var mapoptions = {
zoom: 13,center: new google.maps.LatLng(38.583958,68.780528),mapTypeId: google.maps.MapTypeId.ROADMAP,gestureHandling: "greedy",fullscreenControl: false,disableDefaultUI: true,zoomControl: true,};
map = new google.maps.Map(document.getElementById('map_canvas'),mapoptions);
darection.setMap(map);
google.maps.event.addListener(map,'dragend',function() {
var centeral = map.getcenter();
//alert(centeral);
var names = centeral.toString();
var names =names.substr(1);
names = names.substring(0,names.length - 1);
console.log(names);
var re = /\s*,\s*/;
var nameList = names.split(re);
document.getElementById('bpos_lat').value=nameList[0];
document.getElementById('bpos_lon').value=nameList[1];
source_a = document.getElementById("mypos_lat").value;
source_b = document.getElementById("mypos_lon").value;
source_d = document.getElementById("bpos_lat").value;
source_e = document.getElementById("bpos_lon").value;
var darection = new google.maps.DirectionsRenderer;
var directionsService = new google.maps.DirectionsService;
//darection.setPanel(document.getElementById('panallocation'));
source = source_a + "," + source_b;
destination = source_d + "," + source_e;
var request = {
origin: source,destination: destination,travelMode: google.maps.TravelMode.DRIVING,//Показ алтернативных дорог
provideRouteAlternatives: true
};
directionsService.route(request,function (response,status) {
if (status == google.maps.DirectionsStatus.OK) {
darection.setDirections(response);
}
});
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [source],destinations: [destination],unitSystem: google.maps.UnitSystem.METRIC,avoidHighways: false,avoidTolls: false
},status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
var distance = response.rows[0].elements[0].distance.text;
var duration = response.rows[0].elements[0].duration.text;
distancefinel = distance.split(" ");
//start_addressfinel = start_address.split(" ");
// $('#distance').val(distancefinel[0]);
console.log(distancefinel[0]);
document.getElementById("distancesa").value = distancefinel[0];
////////// IN THIS STATE I WANT DRAW LINE ///////////////////
} else {
alert("Unable to find the distance between selected locations");
}
});
}
);
$('<div/>').addClass('centerMarker').appendTo(map.getDiv())
//do something onclick
.click(function(){
var that=$(this);
if(!that.data('win')){
that.data('win',new google.maps.InfoWindow({content:'this is the center'}));
that.data('win').bindTo('position',map,'center');
}
that.data('win').open(map);
});
}
google.maps.event.addDomListener(window,'load',initialize);