2015-09-19 51 views
1

出於某種原因,我可以動態地將更多的熱圖值添加到我的小冊子js地圖中。代碼錯誤:setInterval(heat.setLatLngs([randomCoords.lat,randomCoords.lng]),200)。我真的知道我做錯了什麼,我正在黑客馬拉松,所以我需要幫助。Leaflet Js熱圖不會動態添加值

$(document).ready -> 

    lat = 0 
    long = 0 
    items = [ 
    [28.417663,-81.581212], 
    [28.417663,-81.581212], 
    [28.417663,-81.581212], 
    [28.417663,-81.581212], 
    [28.417663,-81.581212], 
    [28.417663,-81.581212], 
    ] 

    heat = "" 

    randomCoords = -> 
    coords = {} 
    coords["lat"] = Math.random()/10.0 + 28.417663 
    coords["lng"]= Math.random()/10.0 + -81.581212 

    console.log(coords.lat, coords.lng) 
    return coords 

    getLocation = -> 
    if navigator.geolocation 
     navigator.geolocation.getCurrentPosition showPosition 
     console.log("hello") 
    return 

    showPosition = (position) -> 
    lat = position.coords.latitude 
    long = position.coords.longitude 
    map.setView([28.417663,-81.581212], 13) 
    L.marker([28.417663,-81.581212]).addTo(map).bindPopup("You are here!").openPopup() 
    heat = L.heatLayer(items, { maxZoom: 20 }).addTo(map); 
    setInterval(heat.setLatLngs([randomCoords.lat, randomCoords.lng]), 200) 
    return 


    getLocation() 

    map = L.map('map') 

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', 
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>' 
    maxZoom: 18 
    id: 'id' 
    accessToken: 'token').addTo map 

    # draw = true 
    # add points on mouse move (except when interacting with the map) 
    # map.on 
    # movestart: -> 
    #  draw = false 
    #  return 
    # moveend: -> 
    #  draw = true 
    #  return 
    # mousemove: (e) -> 
    #  if draw 
    #  heat.addLatLng e.latlng 
    #  console.log e.latlng 
    #  return 

    # --- 
    # generated by js2coffee 2.1.0 


    setInterval(randomCoords, 200) 

    # addItems = -> 
    # heat.addLatLng(Math.random + 43.474048, Math.random + -80.5402033); 
    # console.log "added" 
+1

我猜你的hackaton已經結束了,但你甚至沒有提到錯誤是什麼?任何錯誤信息? –

回答

1

是你剛纔寫

setInterval(heat.setLatLngs([randomCoords.lat, randomCoords.lng]), 200) 

我不知道這是否是真正的代碼(我看不到它在您的文章,無論如何),但如果是,那麼

heat.setLatLngs([randomCoords.lat, randomCoords.lng]) 

只要指針伸出代碼就會被調用,然後返回的值將被setInterval使用。

因爲它不是返回的函數(我敢肯定),所以setInterval將失敗。