我是 javascript 新手,所以在地图上绘制折线时遇到问题。我正在从车辆中的 GPS 接收坐标,我想绘制汽车运动。所以我在我的车经过的地方放了标记,但我不知道如何添加折线。有谁能够帮我?
到目前为止,我已经写了这个:
var map;
var markers=[];
var timeout=5000;
var interval=null;
function init2(){
var myLatlng = new google.maps.LatLng(45.7997016667,15.97143);
var mapOptions = {
zoom: 14,
center: myLatlng,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
update_markers();
}
function update_markers(){
console.log('update...');
for(var x in markers){
markers[x].setMap(null);
}
markers=[];
$.getJSON('/get',
function(d){
var l=d.length;
for(var x=l-5;x<l;x++){
var f1=parseFloat(d[x].lat);
var f2=parseFloat(d[x].long);
if(f1>100){
continue;
f1=f1/100.0;
f2=f2/100.0;
}
markers.push(new google.maps.Marker({
position: new google.maps.LatLng(f1, f2),
map: map,
title: d[x].time
}));
}
}
);
interval=setTimeout(update_markers, timeout);
}
google.maps.event.addDomListener(window, 'load', init2);
$(function(){
$('#timeout_sel').change(function(){
clearTimeout(interval);
timeout=$(this).val();
update_markers();
});
});
而且我不知道如何将其放入我的代码中:
function initialize() {
var myLatLng = new google.maps.LatLng(0, -180);
var mapOptions = {
zoom: 3,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892)
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
你能给我一些建议吗?第二个代码来自谷歌示例,它使用固定的 latlng,我如何在可能的 update_markers 函数中添加它,以便它使用与标记相同的坐标?
编辑:
var map;
var markers=[];
var timeout=5000;
var interval=null;
var flightPlanCoordinates=[];
function init2(){
var myLatlng = new google.maps.LatLng(45.7997016667,15.97143);
var mapOptions = {
zoom: 14,
center: myLatlng,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
update_markers();
}
function update_markers(){
console.log('update...');
for(var x in markers){
markers[x].setMap(null);
}
markers=[];
$.getJSON('/get',
function(d){
var l=d.length;
for(var x=l-5;x<l;x++){
var f1=parseFloat(d[x].lat);
var f2=parseFloat(d[x].long);
if(f1>100){
continue;
f1=f1/100.0;
f2=f2/100.0;
}
markers.push(new google.maps.Marker({
position: new google.maps.LatLng(f1, f2),
map: map,
title: d[x].time
}));
flightPlanCoordinates = new google.maps.LatLng(f1,f2);
}
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
}
);
interval=setTimeout(update_markers, timeout);
}
google.maps.event.addDomListener(window, 'load', init2);
$(function(){
$('#timeout_sel').change(function(){
clearTimeout(interval);
timeout=$(this).val();
update_markers();
});
});