2016-09-30 122 views
-2

我正在爲石油和天然氣行業編寫一個程序,該程序允許您在現場使用遠程邏輯板來查看泵插孔是打開還是關閉,然後通過4G互聯網傳遞信息。我試圖用一種方式來構建它,這取決於電路板上的警報是否被觸發,地圖上的圖標將呈紅色或綠色。報警的文件路徑可以通過靜態IP達到像例如:使用Google地圖中的'if'語句更改圖標

http://111.111.111.111/var/rmsdata/alarm1

此文件路徑要麼給出的值爲1或0

如何翻譯的0或1該值到一個if語句會根據值更改圖標?

這裏是我的偶像之一代碼:

 function initialize() { 


     var map_canvas = document.getElementById('map_canvas'); 
     var map_options = { 
      center: new google.maps.LatLng(50.242913, -111.195383), 
      zoom: 14, 
      mapTypeId: google.maps.MapTypeId.TERRAIN 


     } 
     var map = new google.maps.Map(map_canvas, map_options); 

     var point  =  new google.maps.LatLng(47.5, -100); 
     var derrick1 =  new google.maps.Marker ({ 
      position: new google.maps.LatLng(50.244915, -111.198540),  
      map: map, 
      icon: 'on.png', 
      size: new google.maps.Size(20, 32), 
      title: '1' 

     }) 



     google.maps.event.addDomListener(window, 'load', initialize); 
    

回答

1

我做指定網址和底座上的響應圖標簡單的Ajax請求。這段代碼沒有經過測試,我可以改進很多。但它可能有希望指向你正確的方向。

function initialize() { 
    var url = 'http://111.111.111.111/var/rmsdata/alarm1'; 
    var map_canvas = document.getElementById('map_canvas'); 
    var map_options = { 
     center: new google.maps.LatLng(50.242913, -111.195383), 
     zoom: 14, 
     mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 
    var map = new google.maps.Map(map_canvas, map_options); 

    // Make an ajax request for the url that you specified above and base your icon on the response. 
    $.get(url, function(response) { 
     var on = true; 

     if (isNaN(response)) { 
      // If the response would contain anything else but a number. 
      console.log('Response is not a number, defaults to "on"'); 
     } else { 
      // Converts the "0" to "false" and anything else to "true". 
      on = !!+response; 
     } 

     var point = new google.maps.LatLng(47.5, -100); 
     var derrick1 = new google.maps.Marker({ 
      position: new google.maps.LatLng(50.244915, -111.198540), 
      map: map, 
      icon: (on) ? 'on.png' : 'off.png', // Shorthand if-statement to determine the icon. Also called Ternary Operator. 
      size: new google.maps.Size(20, 32), 
      title: '1' 

     }) 
    }); 

    google.maps.event.addDomListener(window, 'load', initialize); 
}