我有一个谷歌地图,可以从 linq 源正确地绘制我的位置。我还可以单击标记以查看信息窗口中的内容。
在标记了我所有位置的地图下方,我显示了标记位置数据的列表。我想从该数据创建一个链接,该链接将在地图上打开相应标记的信息窗口。我只能在加载页面时显示所有标记,但不能显示单个标记。
var currentlyOpenedInfoWindow = null;
function init_map(map_canvas_id, lat, lng, zoom, markers, infoWindowContents) {
var myLatLng = new google.maps.LatLng(lat, lng);
var options = {
zoom: zoom,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map_canvas = document.getElementById(map_canvas_id);
var map = new google.maps.Map(map_canvas, options);
if (markers && markers.length > 0) {
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
var marker = new google.maps.Marker(markers[i]);
marker.setMap(map);
bounds.extend(marker.getPosition());
if (infoWindowContents && infoWindowContents.length > i)
createInfoWindow(map, marker, infoWindowContents[i]);
}
map.fitBounds(bounds);
map.setCenter(bounds.getCenter());
}
}
function createInfoWindow(map, marker, infoWindowProperties) {
var info = new google.maps.InfoWindow(infoWindowProperties);
google.maps.event.addListener(marker, 'click', function() {
if (currentlyOpenedInfoWindow != null)
currentlyOpenedInfoWindow.close();
info.open(map, marker);
currentlyOpenedInfoWindow = info;
});
}
数据是从我的页面后面的代码中调用的,并绘制为:
// Loop through each nearby location and build up the JavaScript to place the markers
var locations = new List<string>();
var infoWindowContents = new List<string>();
DataView nearbyLocations = schoolData_Record.DefaultView;
var count = 1;
foreach (DataRowView location in nearbyLocations)
{
locations.Add(string.Format(
@"{{
title: ""{0}"",
position: new google.maps.LatLng({1}, {2}),
icon: ""../../Frontend/Images/v2/GoogleMaps/NumberToImageHandlerCS.ashx?number={3}""
}}",
location["Name"],
location["Latitude"],
location["Longitude"],
count
)
);
infoWindowContents.Add(string.Format(
@"{{
content: ""<div class=\""infoWindow\""><b>Store #{0}</b><br />{1}<br />{2}, {3} {4}</div>""
}}",
location["Name"],
location["StreetAddress"],
location["city"],
location["State"],
location["Zipcode"]
)
);
count++;
}
var locationsJson = "[" + string.Join(",", locations.ToArray()) + "]";
var overlayContentsJson = "[" + string.Join(",", infoWindowContents.ToArray()) + "]";
// Inject the Google Maps script
ClientScript.RegisterStartupScript(this.GetType(), "Google Maps Initialization",
string.Format("init_map('map_canvas', {0}, {1}, 13, {2}, {3});", lat, lng, locationsJson, overlayContentsJson), true);
}