0

I am newbie in OOP and I try to build an object using ajax request. What I need is to get 'responseArray' in JSON format and than work on it.

function adres(adres) {

this.adres_string = adres;
var self = this
$.ajax({
    type: 'POST',
    url: "http://nominatim.openstreetmap.org/search?q="+adres+"&format=json&polygon=0&addressdetails=0",
    success: function(data) {
        self.responseArray = eval('(' + data + ')')

    }

})


//Method returning point coordinates in EPSG:4326 system
this.getLonLat = function() {

    var lonlat = new OpenLayers.LonLat(this.responseArray.lon, this.responseArray.lat);
    return lonlat;

}
}

The problem starts when in appilcation code I write:

var adr = new adres('Zimna 3, Warszawa');
adr.getLonLat();

This returns nothing as there is no time get the response from the server. How to write it properly in the best way? I've read about when().then() method in jQuery. This may be OK for me. I just want to get know best practise

1 Answer 1

1

This is how AJAX works (notice the A-synchronous part). You are right, the moment you call adr.getLonLat() response did not yet came back. This is the design I would suggest: just pass callback function reference to adres constructor:

function adres(adres, callbackFun) {
  //...
  success: function(data) {
    var responseArray = eval('(' + data + ')')
    var lonlat = new OpenLayers.LonLat(responseArray[i].lon, responseArray[i].lat);
    callbackFun(lonlat)
}

and call it like this:

adres('Zimna 3, Warszawa', function(lonlat) {
  //...
})

Few remarks:

  • adres is now basically a function, you don't need an object here.

  • do not use eval to parse JSON, use JSON object.

  • Are you sure you can POST to http://nominatim.openstreetmap.org? You might hit the same origin policy problem

  • where is the i variable coming from in responseArray[i]?

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I use eval and 'i' as this code snipset is just extraction from bigger part. There is possibility than more than one object will be returned. I just forgot to remove [i] (just edited my question). ToS of Nominatim are not very restricted wiki.openstreetmap.org/wiki/Nominatim_usage_policy
@Odoakr: by same origin policy I mean this, nothing about ToS or legal stuff.
Sorry, I didn't get it. This is just for test. If I decide to write an application based on Nominatim I install instance of this on my server.
BWT, How to code it in full object oriented way (I am learning:)) I insist on it as I have more methodes to add: transforming coordinates, panning and zooming map and so on...
@Odoakr: well, the problem is that while your object is constructed, it's not ready to use because AJAX request still didn't arrive. What I suggest is having a factory method that accept callback. This callback will receive fully created object: adres.create(function(newInstance) {...}). Not beautiful, but works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.