在JQuery中为这些变量使用集合值,而不是随机值

Use set values not random ones for these variables in JQuery?

本文关键字:集合 随机 JQuery 变量      更新时间:2023-09-26

此演示在地图上的随机点创建标记: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);