2013-10-21 42 views
0

我試圖創建一個地圖,我可以循環訪問不同位置的數組。然後我想列出一個隨機座標的位置。所以我希望每次我重新加載頁面時,都會隨機爲我放置一個標記!谷歌地圖上的隨機標記Jquery

function initialize() { 
    //here is the starting for the map, where it will begin to show 
    var latlng = new google.maps.LatLng(59.2982762, 17.9970823); 

    var myOptions = { 
     zoom: 13, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
     //below are the markers coordinates, change it to your coordinates 
     var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions); 

     var i = 0; 
     function randAreas() { 
     var flagAreas = (Math.round(Math.random())-0.5); 
    } 

     var flagAreas = [ 

     [59.2967322, 18.0009393], 
     [59.2980245, 17.9971503], 
     [59.2981078, 17.9980875], 
     [59.2982762, 17.9970823], 
     [59.2987638, 17.9917639], 
     [59.2987649, 17.9917824], 
     [59.2987847, 17.9917731], 
     [59.2988498, 17.991684], 
     [59.2988503, 17.9981593], 
     [59.3008233, 18.0041763], 
     [59.3014033, 18.0068793], 
     [59.3016619, 18.0137766] 
     ]; 

    return flagAreas; 

    flagAreas.sort(randAreas); 

     //script counts the array of coordinates 
     //for (var i = 0; i < flagAreas.length; i++) { 
     var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(flagAreas[i], flagAreas[i]), 
      map: map, 
     }); 
     } 

window.onload = initialize; 

</script> 
+0

存在誤差與環路 –

+0

for循環不運行,它被註釋掉了。 – Chilibiff

+0

該代碼比循環更多的錯誤。 – Andy

回答

0

首先最後,確認這一點,向外移動的地圖變量,你需要在你的代碼的其他地方使用該案件的初始化。

var map; 

function initialize() { 

    var latlng = new google.maps.LatLng(59.2982762, 17.9970823); 
    var myOptions = { 
    zoom: 13, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById('map-canvas'), myOptions); 

    var flagAreas = [ 
    [59.2967322, 18.0009393], [59.2980245, 17.9971503], 
    [59.2981078, 17.9980875], [59.2982762, 17.9970823], 
    [59.2987638, 17.9917639], [59.2987649, 17.9917824], 
    [59.2987847, 17.9917731], [59.2988498, 17.991684], 
    [59.2988503, 17.9981593], [59.3008233, 18.0041763], 
    [59.3014033, 18.0068793], [59.3016619, 18.0137766] 
    ]; 

洗掉flagAreas並抓住第一個數組。

var random = flagAreas.sort(function() { return Math.random() - 0.5 })[0]; 

添加標記。

var marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(random[0], random[1]), 
    map: map, 
    }); 

} 

window.onload = initialize; 

Fiddle

+0

我在這一行上得到一個意外的標識符var random = flagAreas.sort(function()Math.random() - 0.5)[0]; – Chilibiff

+0

這可能是因爲缺少大括號。我更新了答案。否則,應該正常工作 - 檢查小提琴。 – Andy

+0

這不起作用,它不會通過數組運行,它只需要數組中的第一個元素併爲該座標設置一個標記,但它不會隨機化 – Chilibiff

0

你的代碼有3個問題。

1)的initialize意外終止而不執行for迴路。

return flagAreas; //Remove this code 

2)

flagAreas[i] //An array returns both lat and lng 

position: new google.maps.LatLng(flagAreas[i], flagAreas[i]) //Wrong 

更換喜歡

position: new google.maps.LatLng(flagAreas[i][0], flagAreas[i][1]) //Correct 

3)刪除,每當你到達最後一個選項。

map: map 

JSFiddle

+0

謝謝。問題不在於for循環,因爲我不使用它。我想讓MathRandom爲我隨機選擇一個座標,然後在頁面加載時在地圖上放置一個標記。 – Chilibiff

+0

對不起,我的錯誤沒有看到大括號。但它不會隨機化,它只需要數組中的第一個座標。 [鏈接](http://jimkollevik.se/google_map/random.html) – Chilibiff