javascript - 如何使用javascript在谷歌地图中的多个标记之间绘制路线图

标签 javascript android google-maps google-maps-api-3

我使用谷歌地图 API 开发了 map 。带有适当标签的标记是可见的。但是如何在多个标记之间绘制一条路线呢?

谷歌地图图像: GoogleMap Image

JS文件代码如下(userhistory.js)

var	marker = []; 
						 $("#map").show();
				       	 map = new google.maps.Map(document.getElementById('map'), {
			      			zoom: 10,
			     		    center: new google.maps.LatLng(17.7254567, 83.3097112),
			     		    mapTypeId: google.maps.MapTypeId.ROADMAP
			    		});

				       	 directionsDisplay.setMap(map);
			    var infowindow = new google.maps.InfoWindow();
			    var i;
				
			    for (i = 0; i < response.length; i++) {
			    	
				    marker = new google.maps.Marker({
			        position: new google.maps.LatLng(response[i].latitude, response[i].longitude),
			        map: map,
			        label: response[i].count
			      	}); 

			      	google.maps.event.addListener(marker, 'click', (function(marker, i) {
			        return function() {
			          infowindow.setContent("Name :"+response[i].name+","+"<br>"+"Date & Time :"+response[i].date+", "+response[i].time+","+"<br>"+"Location :"+response[i].address);
			          infowindow.open(map, marker);
			        }
			      })(marker, i));  
			}

我很困惑,有人可以帮忙吗?

最佳答案

您可以按照此处的示例使用标记作为生成路线的航路点。

https://developers.google.com/maps/documentation/javascript/examples/directions-waypoints

var marker = []; 
$("#map").show();
map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(17.7254567, 83.3097112),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map); 

var startPoint;
var endPoint;
var waypts = [];

var infowindow = new google.maps.InfoWindow();
var i;

for (i = 0; i < response.length; i++) {       
marker = new google.maps.Marker({
    position: new google.maps.LatLng(response[i].latitude, response[i].longitude),
    map: map,
    label: response[i].count
}); 

if (i == 0) {
    startPoint = new google.maps.LatLng(response[i].latitude, response[i].longitude)
}

if (i == response.length - 1) {
    endPoint = new google.maps.LatLng(response[i].latitude, response[i].longitude),
}

if ((i > 0) && (i < response.length - 1)) {
    waypts.push({
        location: ew google.maps.LatLng(response[i].latitude, response[i].longitude),
        stopover: true
    });
}

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
    infowindow.setContent("Name :"+response[i].name+","+"<br>"+"Date & Time :"+response[i].date+", "+response[i].time+","+"<br>"+"Location :"+response[i].address);
    infowindow.open(map, marker);
}
})(marker, i));  
}

calcRoute(startPoint, endPoint, waypts);

//add this function
function calcRoute(start,end,waypts) { 
var request = {
    origin: start,
    destination: end,
    waypoints: waypts,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
}
});
}

关于javascript - 如何使用javascript在谷歌地图中的多个标记之间绘制路线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45343027/

相关文章:

android - 可以在 Google map 上隐藏兴趣点吗?

html - Google map 和 CSS 变换使页面模糊

javascript - react : Uncaught (in promise) Error: Request failed with status code 400

javascript - 如何实现一个没有名字的接口(interface)成员?

javascript - 如何将 v-model 绑定(bind)到包含输入的子组件

javascript - 使用 Web 音频工作集录制音频时出现咔嗒声

android - openldap 2.4 使用 Syncrepl 配置复制

Android - 使用 LocationManager 不提供地理定位

android,计算两地之间的距离,谷歌距离矩阵

android - 如何从另一个应用程序启动应用程序?