在JQuery中为这些变量使用集合值,而不是随机值
Use set values not random ones for these variables in JQuery?
此演示在地图上的随机点创建标记:http://gmap3.net/examples/pan-to-markers.html
我能对演示代码做的最小修改是什么,这样我就可以指定点的经度和纬度,而不是随机的?
注意:我的确切代码与链接中的演示略有不同:
$('#test1').gmap3(
{ action: 'init',
center:{
lat:44.797916,
lng:-93.278046
},
onces: {
bounds_changed: function(){
$(this).gmap3({
action:'getBounds',
callback: function (bounds){
if (!bounds) return;
var southWest = bounds.getSouthWest(),
northEast = bounds.getNorthEast(),
lngSpan = northEast.lng() - southWest.lng(),
latSpan = northEast.lat() - southWest.lat(),
i;
for (i = 0; i < 10; i++) {
add($(this), i, southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
}
}
});
}
}
}
);
});
这是我实际使用的代码。我知道它的技巧,但这只是一个演示。http://smartpeopletalkfast.co.uk/gmap/demo/overlay.html
如果你想创建自己的点集,你需要按照以下几点来做:
// create a set of lat/lng points. Replace x/y with your own lat/lngs
var points = [
[x, y],
[x, y],
[x, y],
[x, y],
[x, y],
[x, y],
[x, y]
];
$('#test').gmap3(
{ action: 'init',
center:[44.797916,-93.278046],
onces: {
bounds_changed: function(){
$(this).gmap3({
action:'getBounds',
callback: function (bounds){
if (!bounds) return;
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
for (var i = 0; i < points.length; i++) {
// instead use the points you previously defined
add($(this), i, points[i][0], points[i][1]);
}
}
});
}
}
}
);
您应该更改
addPantoMarker($(this), i, southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
和你们想要的价值
addPantoMarker($(this), i, southWest.lat() + latSpan * yourLat, southWest.lng() + lngSpan * yourLong);
这里是将标记添加到随机位置的位置:
addPantoMarker($(this), i, southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
使用所需坐标更改第二个和第三个参数。
示例:
addPantoMarker($(this), i, 47.123, 10.123);
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- Meteor-将选定窗体中的对象添加到集合中
- 主干集合重置和解析
- 使用数据上的角度更改设置集合的第一个元素的动画
- 为集合分配大量的模型弹药
- 如何使用backbone.js从集合中获取模型名称
- 如何从集合中随机选择项目
- 主干:集合将随机模型添加到自身
- 为变量分配有限集合中的随机元素
- 根据某些集合创建随机颜色的方法
- 如何从预定义的集合中选择一个随机函数
- 在JQuery中为这些变量使用集合值,而不是随机值
- MongoDB:从一个集合中提取多个随机文档
- Javascript中不重复数字的随机集合
- 从redis集合中获得随机元素,没有重复
- 从Backbone集合中的每个Model中获取随机项
- 随机选择并使用下划线从集合中删除
- 如何实现一个通用集合随机化器
- 猫鼬集合中的随机文档
- Ember.js:从集合中检索随机元素