这两天在做地图这块,一点点js代码,各种坑。第一次接触js,各种难,下面就这几天的研究做一些总结,求坑
在事件监听器中使用闭包
在执行事件监听器时,通常可取的做法是将私有数据和持久性数据附加到对象中。JavaScript 不支持“私有”实例数据,但是支持允许内部函数访问外部变量的闭包。在事件监听器中,闭包非常适用于访问通常不附加到发生事件的对象的变量。
以下示例在事件监听器中使用了函数闭包将加密讯息分配给一组标记。点击每个标记将会看到加密讯息的一部分,该讯息并不包含在标记本身内。
复制代码 代码如下:
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
// Add 5 markers to the map at random locations.
var southWest = new google.maps.LatLng(-31.203405,125.244141);
var northEast = new google.maps.LatLng(-25.363882,131.044922);
var bounds = new google.maps.LatLngBounds(southWest,northEast);
map.fitBounds(bounds);
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
for (var i = 0; i < 5; i++) {
var location = new google.maps.LatLng(southWest.lat() + latSpan * Math.random(),
southWest.lng() + lngSpan * Math.random());
var marker = new google.maps.Marker({
position: location,
map: map
});
var j = i + 1;
marker.setTitle(j.toString());
attachSecretMessage(marker, i);
}
}
// The five markers show a secret message when clicked
// but that message is not within the marker's instance data.
function attachSecretMessage(marker, number) {
var message = ["This","is","the","secret","message"];
var infowindow = new google.maps.InfoWindow(
{ content: message[number],
size: new google.maps.Size(50,50)
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
这段代码是从google官方里面复制下来的
接下来我要实现从数据库中读取经纬度、地址信息,在google map上进行标注并单击标注出现信息
要实现多点标注,以上代码可以参考
复制代码 代码如下:
<script type="text/javascript" >
var map;
window.onload =function load() {
var number='<%=number %>';//获取后台cs的pubilc的number的值
var address = new Array()
var weidu=new Array();
var jingdu=new Array();
var chepai = new Array();
<% for(int i=0;i<number;i++){ %>
weidu.push('<%=lan[i]%>');//从后台cs获取经纬度的值
jingdu.push('<%=lon[i]%>');
address.push('<%=addr[i]%>');
chepai.push('<%=carnum[i] %>');
// var wei ;
// wei = chepai['<%=i%>'];
// alert(wei);
<% } %>
var Latlng = new google.maps.LatLng(weidu[0], jingdu[0]);
// var myLatlng = new google.maps.LatLng(25.273566, 110.290195);
var myOptions = {
zoom: 8,
center: Latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);////初始化一个地图实例
for(var j=0;j<number;j++) //这里卡了好久,原因是js的类型只有var;把js和cs弄混了一直用到《% %>,其实单纯的js不需要,跟C差不多就可以了
{
var lat = weidu[j];
var lng = jingdu[j];
var addre = address[j];
var chepaihao = chepai[j];
// alert(j);
// alert(lat);
// var image = 'Image/webcam.png';
//设置标志点
var myLatlng = new google.maps.LatLng(weidu[j ], jingdu[j ]);
var h = chepaihao;
var tit = h.toString();
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
// icon: "http://google-maps-icons.googlecode.com/files/yellow08.png",
// icon: image, //通过icon可以设定自己想显示的图标,不设置则显示Google map默认的图标
title: tit //title表示鼠标放到marker上要显示的值。
});
attachSecretMessage(marker, j,addre,lat,lng);
}
}
function attachSecretMessage(marker, number,addre1,lat1,lng1) {
// var message = ["This", "is", "the", "secret", "message"];
var infowindow = new google.maps.InfoWindow(
{ content: "<span style='font-size:15px'><b>地址: </b>" + addre1 + "<br>"+ "经纬度:"+ lat1 + ","+ lng1 +"</span>",
size: new google.maps.Size(50, 50)
});
// infowindow.setContent("地址:"+ addre1 +"经度:" +lat1 + "纬度:"+lng1);
//点击弹出信息窗口
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
}
</script>
cs后端代码:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
public double[] la = new double[25];
public double[] ln = new double[25];
public int[] id = new int[25];
public string[] addr = new string[25];
public int number;
protected void Page_Load(object sender, EventArgs e)
{
string mycnnConnectionString1 = System.Configuration.ConfigurationManager.ConnectionStrings["testconect"].ConnectionString; //创建连接字符串
SqlConnection mycnn1 = new SqlConnection(mycnnConnectionString1);
mycnn1.Open();
SqlCommand cmd1 = new SqlCommand("select L_ID,L_Lantitude,L_Longitude,L_Address from LatestPosition ", mycnn1);
SqlDataReader dr1 = cmd1.ExecuteReader();
int k2 = 0;
int k3 = 0;
int k4 = 0;
int k1 = 0;
// string buf1 = "";
double buf2 = 0;
double buf3 = 0;
int buf4 = 0;
string buf1 = "0";
while (dr1.Read())
{
//lat
buf2 = (double)dr1["L_Lantitude"];
la[k2] = buf2;
k2++;
//lng
buf3 = (double)dr1["L_Longitude"];///数据库读取float型的数据要用double
ln[k3] = buf3;
k3++;
//id
buf4 = (int)dr1["L_ID"];
id[k4] = buf4;
k4++;
buf1 = dr1["L_Address"].ToString();
addr[k1] = buf1;
k1++;
}
number = k4;
dr1.Close();
mycnn1.Close();
}
}
在事件监听器中使用闭包
在执行事件监听器时,通常可取的做法是将私有数据和持久性数据附加到对象中。JavaScript 不支持“私有”实例数据,但是支持允许内部函数访问外部变量的闭包。在事件监听器中,闭包非常适用于访问通常不附加到发生事件的对象的变量。
以下示例在事件监听器中使用了函数闭包将加密讯息分配给一组标记。点击每个标记将会看到加密讯息的一部分,该讯息并不包含在标记本身内。
复制代码 代码如下:
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
// Add 5 markers to the map at random locations.
var southWest = new google.maps.LatLng(-31.203405,125.244141);
var northEast = new google.maps.LatLng(-25.363882,131.044922);
var bounds = new google.maps.LatLngBounds(southWest,northEast);
map.fitBounds(bounds);
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
for (var i = 0; i < 5; i++) {
var location = new google.maps.LatLng(southWest.lat() + latSpan * Math.random(),
southWest.lng() + lngSpan * Math.random());
var marker = new google.maps.Marker({
position: location,
map: map
});
var j = i + 1;
marker.setTitle(j.toString());
attachSecretMessage(marker, i);
}
}
// The five markers show a secret message when clicked
// but that message is not within the marker's instance data.
function attachSecretMessage(marker, number) {
var message = ["This","is","the","secret","message"];
var infowindow = new google.maps.InfoWindow(
{ content: message[number],
size: new google.maps.Size(50,50)
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
这段代码是从google官方里面复制下来的
接下来我要实现从数据库中读取经纬度、地址信息,在google map上进行标注并单击标注出现信息
要实现多点标注,以上代码可以参考
复制代码 代码如下:
<script type="text/javascript" >
var map;
window.onload =function load() {
var number='<%=number %>';//获取后台cs的pubilc的number的值
var address = new Array()
var weidu=new Array();
var jingdu=new Array();
var chepai = new Array();
<% for(int i=0;i<number;i++){ %>
weidu.push('<%=lan[i]%>');//从后台cs获取经纬度的值
jingdu.push('<%=lon[i]%>');
address.push('<%=addr[i]%>');
chepai.push('<%=carnum[i] %>');
// var wei ;
// wei = chepai['<%=i%>'];
// alert(wei);
<% } %>
var Latlng = new google.maps.LatLng(weidu[0], jingdu[0]);
// var myLatlng = new google.maps.LatLng(25.273566, 110.290195);
var myOptions = {
zoom: 8,
center: Latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);////初始化一个地图实例
for(var j=0;j<number;j++) //这里卡了好久,原因是js的类型只有var;把js和cs弄混了一直用到《% %>,其实单纯的js不需要,跟C差不多就可以了
{
var lat = weidu[j];
var lng = jingdu[j];
var addre = address[j];
var chepaihao = chepai[j];
// alert(j);
// alert(lat);
// var image = 'Image/webcam.png';
//设置标志点
var myLatlng = new google.maps.LatLng(weidu[j ], jingdu[j ]);
var h = chepaihao;
var tit = h.toString();
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
// icon: "http://google-maps-icons.googlecode.com/files/yellow08.png",
// icon: image, //通过icon可以设定自己想显示的图标,不设置则显示Google map默认的图标
title: tit //title表示鼠标放到marker上要显示的值。
});
attachSecretMessage(marker, j,addre,lat,lng);
}
}
function attachSecretMessage(marker, number,addre1,lat1,lng1) {
// var message = ["This", "is", "the", "secret", "message"];
var infowindow = new google.maps.InfoWindow(
{ content: "<span style='font-size:15px'><b>地址: </b>" + addre1 + "<br>"+ "经纬度:"+ lat1 + ","+ lng1 +"</span>",
size: new google.maps.Size(50, 50)
});
// infowindow.setContent("地址:"+ addre1 +"经度:" +lat1 + "纬度:"+lng1);
//点击弹出信息窗口
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
}
</script>
cs后端代码:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
public double[] la = new double[25];
public double[] ln = new double[25];
public int[] id = new int[25];
public string[] addr = new string[25];
public int number;
protected void Page_Load(object sender, EventArgs e)
{
string mycnnConnectionString1 = System.Configuration.ConfigurationManager.ConnectionStrings["testconect"].ConnectionString; //创建连接字符串
SqlConnection mycnn1 = new SqlConnection(mycnnConnectionString1);
mycnn1.Open();
SqlCommand cmd1 = new SqlCommand("select L_ID,L_Lantitude,L_Longitude,L_Address from LatestPosition ", mycnn1);
SqlDataReader dr1 = cmd1.ExecuteReader();
int k2 = 0;
int k3 = 0;
int k4 = 0;
int k1 = 0;
// string buf1 = "";
double buf2 = 0;
double buf3 = 0;
int buf4 = 0;
string buf1 = "0";
while (dr1.Read())
{
//lat
buf2 = (double)dr1["L_Lantitude"];
la[k2] = buf2;
k2++;
//lng
buf3 = (double)dr1["L_Longitude"];///数据库读取float型的数据要用double
ln[k3] = buf3;
k3++;
//id
buf4 = (int)dr1["L_ID"];
id[k4] = buf4;
k4++;
buf1 = dr1["L_Address"].ToString();
addr[k1] = buf1;
k1++;
}
number = k4;
dr1.Close();
mycnn1.Close();
}
}
标签:
js闭包,多点标注冒泡
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“用js闭包的方法实现多点标注冒泡示例”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?