我正在使用Google地图API V3处理位置参考页面,并且将有大约30个标记。我不确定在html页面上是否有所有标记将是托管的,或者它是否应该存储在不同的位置。还包括标记信息(描述)。
我使用Google Maps API V3找到了多个标记的代码,并使用Google自己的参考修改了它。这是代码:
<script type="text/javascript">
var infoString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Auckland 6625</h1>'+
'<div id="bodyContent">'+
'<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the '+
'Northern Territory, central Australia. It lies 335 km (208 mi) '+
'south west of the nearest large town, Alice Springs; 450 km '+
'(280 mi) by road. Kata Tjuta and Uluru are the two major '+
'features of the Uluru - Kata Tjuta National Park. Uluru is '+
'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
'Aboriginal people of the area. It has many springs, waterholes, '+
'rock caves and ancient paintings. Uluru is listed as a World '+
'Heritage Site.</p>'+
'<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
'(last visited June 22, 2009).</p>'+
'</div>'+
'</div>'
var locations = [
[infoString, -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-39.92, 151.25),
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
var markers = new Array();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
function AutoCenter() {
// Create a new viewpoint bound
var bounds = new google.maps.LatLngBounds();
// Go through each...
$.each(markers, function (index, marker) {
bounds.extend(marker.position);
});
// Fit these bounds to the map
map.fitBounds(bounds);
}
AutoCenter();
</script>
由于标记信息(描述)会很长,(不像上面所示)我想为每个标记创建一个变量。
所以我不确定这是否是最有效的方法。请帮忙。
答案 0 :(得分:0)
现在你的做法没有任何低效率。虽然将每个描述作为一个单独的变量没有真正的优势,但您也可以从一开始就让它成为locations
变量的一部分。除非您从其他地方获取数据(例如ajax请求),否则您可以将其添加到location
。
我所做的唯一真正的改变不是拥有2D数组,而是让它成为一维对象数组。
var locations = [
{
description: infoString,
lat: -33.890542,
lng: 151.274856,
foo: 4
},
{description: 'Coogee Beach', lat: -33.923036, lng: 151.259052, foo: 5},
{description: 'Cronulla Beach', lat: -34.028249, lng: 151.157507, foo: 3},
{description: 'Manly Beach', lat: -33.80010128657071, lng: 151.28747820854187, foo: 2},
{description: 'Maroubra Beach', lat: -33.950198, lng: 151.259302, foo: 1}
];
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].lat, locations[i].lng),
map: map
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i].description);
infowindow.open(map, marker);
}
})(marker, i));
}