2016-08-30 7 views
-1

JSONデータをajax呼び出しを使用して変数に格納します。どのようにajax呼び出しを記述し、JSONデータ値を変数に保存してGoogleマップのデータを表示するか。 JSONのDustValueが必要で、Google Maps jsの変数に割り当てる必要があります。ajax呼び出しを使用してvaribeにjsonデータを格納する方法

<head> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCLi6lY_WbRa9mkWKc" type="text/javascript"></script> 
    <script> 
     var myCenter = new google.maps.LatLng(22.8046, 86.2029); 
     function initialize() 
     { 
      var mapProp = { 
       center:myCenter, 
       zoom:17, 
       mapTypeId:google.maps.MapTypeId.ROADMAP 
      }; 

      var map=new google.maps.Map(document.getElementById("googleMap"),mapProp); 
      var DustValue=23;// ajax call.. $.ajax({url: 'http://xyz.in/dust/get'...... 
      var dustbinstatus=''; 

      if(DustValue<50){ 
       dustbinstatus = 'img/dustbinempty.png'; 
      } else if(DustValue>50 && DustValue<90){ 
       dustbinstatus = 'img/dustbinfull.png'; 
      } else if(DustValue>90){ 
       dustbinstatus = '3.jpg'; 
      } 

      var marker=new google.maps.Marker({ 
       position:myCenter, 
       icon: v_icon 
      }); 

      marker.setMap(map); 
     } 

     google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 
</head> 

<body> 
    <div id="googleMap" style="width:1580px;height:780px;"></div> 
</body> 

サンプルデータ:

{"success":true,"sensorsdata":{"WaterTemperature":null,"DustValue":23.00,"DOValue":null,"CurrentTime":"29-August-2016 02:59:AM"}} 

答えて

0

あなたは変数に応答を格納するsuccessコールバックハンドラを使用する必要があるが、ここでは一例であり、

var dustValue;  
    $.ajax({ 
      type: "GET", 
      url: "your_url_goes_here", 
      dataType: "json", 
      success : function(data) { 
       dustValue = data; 
      } 
     }); 

は、この情報がお役に立てば幸いです!

関連する問題