我编写了一部分代码,用于在Google地图上放置迭代标记:
var markers = [];
var coordinates = [];
var interest_points = [];
var joined_data = [];
var map = null;
var key_obj = new Map();
var markers = [];
var directionsDisplay = [];
var directionsService = new google.maps.DirectionsService();
var REQUEST_WAYPOINTS_LIMIT = 8;
function placeMarker(lat, lon, icon_path)
{
var markerPos = new google.maps.LatLng(lat, lon);
var marker = new google.maps.Marker
(
{
position: markerPos,
map: map,
//animation: google.maps.Animation.DROP,
icon: icon_path
}
);
markers.push(marker);
}
function placeAttractionMarker(lat, lon, icon_path, name, url, id)
{
var contentString = '<div id="content"><div id="siteNotice">' +
'</div><div id="bodyContent"><h4 id="firstHeading" class="firstHeading">' + name + '</h4>' +
'<a href="' + url + '" style="text-decoration:none" target="_blank"><b>Sito web</b></a></br>' +
'<a href="" onclick="addMarkerToWps(this.id)" id="' + id +
'">Aggiungi attrazione al percorso</a></div></div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var markerPos = new google.maps.LatLng(lat, lon);
var marker = new google.maps.Marker
(
{
position: markerPos,
map: map,
//animation: google.maps.Animation.DROP,
icon: icon_path,
title: name
}
);
marker.addListener('click', function() {
infowindow.open(map, marker);
});
markers.push(marker);
}
我使用了两种不同的功能,因为我需要放置两种不同的标记。从代码中可以看出,某些标记在单击时会显示气球。在气球内部,有一个URL链接,我只需要在点击链接时启动功能 addMarkerToWps :
function setMapOnAll(map_name) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map_name);
}
//markers = [];
}
function addMarkerToWps(wp_id)
{
setMapOnAll(null);
}
无论如何标记都不会消失,即使看起来地图已重新加载。
编辑:放置标记并在地图上计算路径的函数
function calculateRoutes(reDraw)
{
// remove previous route
removeRoutes();
//window.alert('Numero punti ' + coordinates.length);
// set start position
//var begin = new google.maps.LatLng(coordinates[0].Lat, coordinates[0].Lon);
var begin = new google.maps.LatLng(joined_data[0].Lat, joined_data[0].Lon);
// set end position
var end = null;
// waypoints struct
var waypoints = [];
// counter
var wCount = 0;
// loop
var i;
//for(i=1; i<coordinates.length-1; i++)
for(i=1; i<joined_data.length-1; i++)
{
if(wCount === REQUEST_WAYPOINTS_LIMIT)
{
// increment counter to get end's index
i++;
// set end
//end = new google.maps.LatLng(coordinates[i].Lat, coordinates[i].Lon);
end = new google.maps.LatLng(joined_data[i].Lat, joined_data[i].Lon);
// draw current route
drawRoute(begin, end, waypoints);
// update indeces
begin = end;
// reset structures
wCount = 0;
waypoints = [];
}
// add waypoint to list
waypoints.push
(
{
//location: new google.maps.LatLng(coordinates[i].Lat, coordinates[i].Lon),
location: new google.maps.LatLng(joined_data[i].Lat, joined_data[i].Lon),
stopover: true
}
);
// increments counter
wCount++;
}
if(waypoints.length > 0)
{
//end = new google.maps.LatLng(coordinates[coordinates.length-1].Lat, coordinates[coordinates.length-1].Lon);
end = new google.maps.LatLng(joined_data[joined_data.length-1].Lat, joined_data[joined_data.length-1].Lon);
drawRoute(begin, end, waypoints);
}
if(!reDraw)
{
for(var i = 0; i<coordinates.length; i++)
{
placeMarker(coordinates[i].Lat, coordinates[i].Lon, "http://gmaps-samples.googlecode.com/svn/trunk/markers/red/blank.png");
}
for(var j = 0; j<interest_points.length; j++)
{
placeAttractionMarker(
interest_points[j].Lat,
interest_points[j].Lon,
"http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png",
interest_points[j].Name,
interest_points[j].Wikipedia,
j
);
}
}
else
{
for(var i = 0; i<coordinates.length; i++)
{
placeMarker(coordinates[i].Lat, coordinates[i].Lon, "http://gmaps-samples.googlecode.com/svn/trunk/markers/red/blank.png");
}
key_obj.forEach(function(value, key) {
console.log(key + " = ");
}, key_obj)
}
你能告诉我为什么吗?
由于