1
Hello, I’m having trouble accessing objects from a json array that is passed as a parameter between functions of two classes. In the functions that receive the json array, the corresponding variables are as "Undefined".
I have three files:json_control.js(Class) ; map_control.js and js map.(Class)
What I do is: access the json array that is saved in a postgresql database through json_control, return that array to the mapa_controler.js and forward to the js map. so that it can place the array objects on the map.
json_control.js
function JsonControl() {};
JsonControl.prototype.getJsonData = function(dataLocation){
$.getJSON(dataLocation, function(mData) {
return mData;
});
};
map_control.js
var mJsonControl = new JsonControl();
var mMap = new Mapa();
function initialize() {
var json = mJsonControl.getJsonData("consultas.php");
mMap.setMap();
mMap.setMapElements(json);
}
google.maps.event.addDomListener(window, 'load',initialize);
js map.
function Mapa() {
this.map = {};
this.mapCanvas = 'map';
this.mapOptions = {center:{lat: -22.717, lng: -42.624}, zoom: 14};
};
Mapa.prototype.setMapElements = function (mJsonData) {
var mMap = this.map;
var elementOptions = {};
$.each(mJsonData, function(id,data){
elementOptions = {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: mMap,
center: new google.maps.LatLng(data.latitude, data.longitude),
radius: Math.sqrt(1)*10
};
new google.maps.Circle(elementOptions);
});
};
Mapa.prototype.setMap = function () {
this.map = new google.maps.Map(document.getElementById(this.mapCanvas), this.mapOptions);
};
This is the json array returned by the page php queries.:
[{"id":"2","age":"14","sex":"F","latitude":"-22.716","longitude":"-42.638"},{"id":"7","age":"35","sex":"M","latitude":"-22.7158","longitude":"-42.6289"},{"id":"0","age":"20","sex":"F","latitude":"-22.7197","longitude":"-42.6145"},{"id""":"1","age":"30","sex":"M","latitude":"M","latitude"-22.7187","longitude":"-42.6155"},{"id":"3","age":"25","sex":"F","latitude":"-22.7086","longitude":"-42.6213"}]
I would like a solution that maintains the responsibilities of each class.
I made the modifications here. It worked perfectly!
– regmoraes