我有以下代码:
它基本上查看HTML列表和地理编码,并标记每个项目。它十有八九是正确的,但有时我会在控制台中得到一个设置为show的错误。
我什么都想不出来。任何想法都很感激。
$(function () {
var map = null;
var geocoder = null;
function initialize() {
if (GBrowserIsCompatible()) {
// Specifies that the element with the ID map is the container for the map
map = new GMap2(document.getElementById("map"));
// Sets an initial map positon (which mainly gets ignored after reading the adderesses list)
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
// Instatiates the google Geocoder class
geocoder = new GClientGeocoder();
map.addControl(new GSmallMapControl());
// Sets map zooming controls on the map
map.enableScrollWheelZoom();
// Allows the mouse wheel to control the map while on it
}
}
function showAddress(address, linkHTML) {
if (geocoder) {
geocoder.getLatLng(address,
function (point) {
if (!point) {
console.log('Geocoder did not return a location for ' + address);
}
else {
map.setCenter(point, 8);
var marker = new GMarker(point);
map.addOverlay(marker);
// Assigns the click event to each marker to open its balloon
GEvent.addListener(marker, "click", function () {
marker.openInfoWindowHtml(linkHTML);
});
}
}
);
}
} // end of show address function
initialize();
// This iterates through the text of each address and tells the map
// to show its location on the map. An internal error is thrown if
// the location is not found.
$.each($('.addresses li a'), function () {
var addressAnchor = $(this);
showAddress(addressAnchor.text(), $(this).parent().html());
});
});
查看以下HTML:
<ul class="addresses">
<li><a href="#">Central London</a></li>
<li><a href="#">London WC1</a></li>
<li><a href="#">London Shoreditch</a></li>
<li><a href="#">London EC1</a></li>
<li><a href="#">London EC2</a></li>
<li><a href="#">London EC3</a></li>
<li><a href="#">London EC4</a></li>
</ul>