I have this script I am trying to use to search for a value within a CartoDB table. I can't seem to figure out how to reverse the order of the lat & long coordinates so they appear as lonlat... instead of latlon

Here is what I am trying to do:

function parcelSearch(layer) {
        var sql = new cartodb.SQL({ user: 'dchadney' });
        var $options = $('#searchParcel');
        $options.click(function(e) {
            var streetNam = document.getElementById('parcelid').value;
            if(streetNam === "") {
                alert("Please enter the parcel ID");
            } else {
                query = "SELECT * FROM parcels_sa WHERE parcel_id ILIKE '%" + streetNam + "%'";

                sql.getBounds(query).done(function(bounds) {
                    latlon = bounds[0];

                   // map.fitBounds(bounds);

                    setTimeout(function() {
                    backdrop: 'static'
                    }, 7000);
                    $('.line2').css('width', '42%');  

            // change the query in the layer to update the map


Seems to me that latlon is a function coming from leaflet? How do I modify this?


I'm pretty sure the CartoDB javascript API is the Leaflet API, so have a look at their doco: http://leafletjs.com/reference.html#latlng

You can either just pull the values out of there, something like this:

var coord = L.latLng(50.5, 30.5);
var lng = coord.lng;
var lat = coord.lat;

Or you could store it as your own object, like this:

var lnglat = {lng:12.33, lat:44.11};
var lat = lnglat.lat;
var lng = lnglat.lng;

But perhaps if you describe what you're actually trying to do, there will be a more elegant way of doing it than reversing the values?

  • What's your actual problem? Does the map.fitbounds function not work? Or are you trying to store lat/lng values somewhere? – Alex Leith Jul 22 '15 at 1:08
  • Yes I am trying to send the lat & long values to a database in CartoDB. The database requires the longitude value to come first. So this code takes parcel id input from the user and searches a cartodb database for the value, returns this along with the lat long coordinates... – dchadney Jul 22 '15 at 14:55
  • Hmm. Can you point to a reference to the insert statement doco for CartoDB? If they are returning a bounding box with two latlng coordinates, I would expect it to accept a latlng as a point to insert. Otherwise, just get the values out as I've shown above and build the data structure CartoDB needs! – Alex Leith Jul 22 '15 at 22:38

You can access to the specific coordinates of the bounds this way:


But take into account that in the bounds you'll have two coordinates that form the two corners of the bounding box, so perhaps you want to try to get the center of the box by using:

  • okay so if that is logging the bounds to the console, how do I then get it from the console to use? – dchadney Jul 24 '15 at 14:35
  • I'm just sending you an email right now, but you can use bounds[0][0] and the rest of them directly in your JS code :-) – iriberri Jul 24 '15 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.