'Ajax response into django
I have a map with some markers on them and im using AJAX to bring up the latest measures from the database. Problem is when i assign my called information it messes up with the marker positioning . If i put the Popup outside of the ajax call i cant access my measurements. If i assign them in my ajax call it ruins the gui but i get the correct results.
for (i in motePos) {
mote[i] = L.marker(motePos[i], {
icon: SensorIcon
});
mote[i].name = motePosName[i];
mote[i].id = motePosId[i];
markers.addLayer(mote[i].on('click', function(e) {
alert(this.id);
var sID = this.id;
$.ajax({
type : 'GET',
url : "{% url 'agriculture:sensor_ms' %}",
data : {"sID" : sID},
success: function(response){
var measurement = JSON.parse(response["measurement"]);
var fields = measurement[0]["fields"];
console.log(fields)
var sensorID = "href='{% url 'agriculture:tree_sensor' 12345 %}'";
var url_mask = sensorID.replace("12345", fields["sensor"]);
mote[i].bindPopup("<h3>Tree Sensor: " + motePosName[i] + "</h3>"
+ "<hr>" + "Soil Moisture (50 cm): " + fields["soil_moisture_depth_1"] + "cbar"
+ "<br>" + "<b>Soil Moisture (20 cm): " + fields["soil_moisture_depth_2"] + "cbar"
+ "<br>" + "Temperature: " + fields["soil_temperature"] + "°C"
+ "<br>" + "<br><a class='popup_button small_text' "+url_mask+" ><span>Sensor Page</span></a>").openPopup();
}
})
}));
}
map.addLayer(markers);
view :
def Sensor_Measurements(request):
if request.is_ajax and request.method == "GET" :
sID = request.GET.get("sID")
tree_sensors = TreeSensor.objects.filter(pk = sID)
late = TreeSensorMeasurement.objects.filter(sensor_id__in = tree_sensors).latest('datetime')
measurement = serializers.serialize('json',[late])
return JsonResponse({"measurement": measurement}, status=200)
return JsonResponse({"error": ""}, status=400)
How do i take the response out of the ajax and assign it after that ? basically how to do something like :
for (i in motePos) {
mote[i] = L.marker(motePos[i], {
icon: SensorIcon
});
mote[i].name = motePosName[i];
mote[i].id = motePosId[i];
markers.addLayer(mote[i].on('click', function(e) {
var sID = this.id;
$.ajax({
...
success: function(response){
var measurement = JSON.parse(response["measurement"]);
var fields = measurement[0]["fields"];
}
})
var sensorID = "href='{% url 'agriculture:tree_sensor' 12345 %}'";
var url_mask = sensorID.replace("12345", fields["sensor"]);
mote[i].bindPopup("<h3>Tree Sensor: " + motePosName[i] + "</h3>"
+ "<hr>" + "Soil Moisture (50 cm): " + fields["soil_moisture_depth_1"] + "cbar"
+ "<br>" + "<b>Soil Moisture (20 cm): " + fields["soil_moisture_depth_2"] + "cbar"
+ "<br>" + "Temperature: " + fields["soil_temperature"] + "°C"
+ "<br>" + "<br><a class='popup_button small_text' "+url_mask+" ><span>Sensor Page</span></a>").openPopup();
}));
}
map.addLayer(markers);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
