2012-03-05 27 views
0

該演示創建隨機點在地圖上的標記: http://gmap3.net/examples/pan-to-markers.html在JQuery中爲這些變量使用設置值而不是隨機的值?

請告訴我我可以對演示代碼最小的修改,所以我可以指定點的經度和緯度,而不是讓他們隨機的?

記下確切的代碼我是在鏈路演示稍有不同:

$('#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

+1

你嘗試什麼嗎? – 2012-03-05 14:19:24

+0

我意識到ive粘貼了錯誤的代碼,ive更新了我的問題。謝謝 – Evans 2012-03-05 16:53:01

回答

2

如果你想創建自己的點的集合,你會想要做的大意如下的內容:

// 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]); 
      } 
     } 
     }); 
    } 
    } 
} 
); 
+0

對不起,我的代碼實際上與我發佈的代碼有點不同。我沒有addPantoMarker,我更新了我的問題。謝謝 – Evans 2012-03-05 14:39:40

+0

的確,它只是叫做「add」。我改變了我的代碼以反映這一點。 – Kezzer 2012-03-05 21:38:08

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); 
+0

這是錯的。他想要指定確切的座標。你用另一個單值'yourLat'和'yourLong'代替'Math.random()'。 – papaiatis 2012-03-05 14:23:09

+0

對不起,我的代碼實際上與我發佈的代碼有點不同。我沒有addPantoMarker,我更新了我的問題。謝謝 – Evans 2012-03-05 14:40:05

0

在這裏你添加標記到隨機位置:

addPantoMarker($(this), i, southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());

更改第二和第三個參數與所需的座標。

例子:

addPantoMarker($(this), i, 47.123, 10.123);

+0

對不起,我的代碼實際上與我發佈的代碼有點不同。我沒有addPantoMarker,我更新了我的問題。謝謝 – Evans 2012-03-05 14:39:52