作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 MySQL 数据库中检索纬度和经度,并使用 Google map 使用标记显示检索到的坐标的位置,我正在尝试做这样的事情:
无效 Page_Load:
int RowLength;
String[] lon = null;
String[] lat = null;
protected void Page_Load(object sender, EventArgs e)
{
using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection("Server;Port;Database=DB;Uid=U;Pwd=P"))
{
connection.Open();
MySql.Data.MySqlClient.MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT longitude,latitude FROM table";
MySql.Data.MySqlClient.MySqlDataReader datr = cmd.ExecuteReader();
lon = new string[datr.FieldCount];
lat = new string[datr.FieldCount];
rep_loc = new string[datr.FieldCount];
RowLength = datr.FieldCount;
while (datr.Read())
{
lon[counter] = datr[0].ToString();
lat[counter] = datr[1].ToString();
counter++;
}
datr.Close();
connection.Close();
}
}
Google map 的 Javascript
<script type="text/javascript">
var lon;
var lat;
var homeLatlng;
var mapOptions;
var map;
var contentString;
var infowindow;
function initialize() {
//I AM STUCK OVER HERE! and Don't know how to set multiple markers with information window for each marker
for(var i=0;i<='<%=RowLength%>';i++){
homeLatlng = new google.maps.LatLng('<%=lat[0]%>', '<%=lon[0]%>');//initial
mapOptions = {
zoom: 5,
center: homeLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
marker = new google.maps.Marker({
position: homeLatlng,
map: map,
url: "http://www.google.com",
});
contentString = '<div id="content" style="color:#000000;">' +
'This is a test <br>' +
'<a href="www.google.com"> google </a> <br>' +
'</div>';
infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function () { infowindow.open(map, marker) });
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
有什么想法吗?
谢谢
最佳答案
我在 javascript 中使用以下代码完成了此操作:
<script type="text/javascript">
var lat = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lat)%>;
var lon = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lon)%>;
function initialize() {
mapOptions = {
zoom: 4,
center: new google.maps.LatLng(lat[0],lon[0]),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var len = '<%=RowLength%>';
for (i = 0; i <= len ; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat[i], lon[i]),
map: map,
url: "http://www.google.com",
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent("<div style='color:#000000';> Testing </div>");
infowindow.open(map, marker);
}
})(marker, i));
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
关于c# - 在谷歌地图上绘制多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045278/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!