7

Hey I am in the middle of a project and I suddenly realized that my layer is projected somewhere else on the globe. After some experimentation I've realized that this is because the program translates lng for lat. I really don't understand why is that, but at the same time I need to solve the issue ASAP, as time is running out.

Case: [37.994, 24.75906] is being read as [24.75906, 37.994]

I have used the e.latlng method to acquire the data so it really puzzles me that the GeoJSON reads coordinates in reverse.

Edit: OK it turns out that this is a known issue of conflicting standards. Now the issue remains: How can I quickly resolve this since I already have hundreds of JSON [y,x] couples? Last resort for me will be regex but I would like to avoid it.

Do you have any ideas on how to solve quickly the issue? I have hundreds of data like that.

Note: if you know this to be a duplicate please leave a comment and I'll delete the question.

2

2 Answers 2

13

For the record, in case you end up in such situation where you have a lot of GeoJSON data in hand but the lat-lng coordinates order is reversed, a very easy workaround within Leaflet is to use the coordsToLatLng option of the L.geoJSON factory:

L.geoJSON(geoJsonData, {
    coordsToLatLng: function (coords) {
        //                    latitude , longitude, altitude
        //return new L.LatLng(coords[1], coords[0], coords[2]); //Normal behavior
        return new L.LatLng(coords[0], coords[1], coords[2]);
    }
});

Of course the definitive solution is to find a way to correct the original GeoJSON data.

See also this post on SO: Leaflet: how to swap coordinates received from an ajax call

1
  • I ve tried to upvote but I dont have the necessary reputation yet! This is the kind of answer I was looking for last week, ty. Commented Jul 10, 2017 at 4:28
0

Can't wait any longer:

var a = "[LatLng(37.43943, 25.30563)],[LatLng(37.4367, 25.30495)],[LatLng(37.43071, 25.29945)],[LatLng(37.42362, 25.30426)],[LatLng(37.42089, 25.31113)],[LatLng(37.41489, 25.31113)],[LatLng(37.41053, 25.30769)],[LatLng(37.40235, 25.30563)],[LatLng(37.40562, 25.31525)],[LatLng(37.41816, 25.31937)],[LatLng(37.42307, 25.3228)],[LatLng(37.40889, 25.33104)],[LatLng(37.4078, 25.33722)],[LatLng(37.41598, 25.33791)],[LatLng(37.40344, 25.35027)],[LatLng(37.40726, 25.3688)],[LatLng(37.42253, 25.39009)],[LatLng(37.4138, 25.39902)],[LatLng(37.42907, 25.4052)],[LatLng(37.42961, 25.41962)],[LatLng(37.44215, 25.42442)],[LatLng(37.44543, 25.45807)],[LatLng(37.46287, 25.4615)],[LatLng(37.47595, 25.46013)],[LatLng(37.47104, 25.42786)],[LatLng(37.48249, 25.42923)],[LatLng(37.48358, 25.41)],[LatLng(37.49502, 25.40588)],[LatLng(37.49447, 25.38597)],[LatLng(37.49066, 25.3791)],[LatLng(37.46941, 25.38254)],[LatLng(37.46178, 25.37773)],[LatLng(37.4754, 25.35988)],[LatLng(37.4972, 25.35782)],[LatLng(37.50047, 25.34203)],[LatLng(37.49774, 25.32761)],[LatLng(37.49229, 25.3228)],[LatLng(37.4912, 25.3125)],[LatLng(37.4814, 25.30907)],[LatLng(37.47268, 25.31113)],[LatLng(37.46341, 25.32692)],[LatLng(37.44597, 25.32692)],[LatLng(37.42798, 25.3228)],[LatLng(37.43943, 25.30563)]".split("],[");
var b = a.map(function(x) { 
    var couple= (/\d+.\d+, \d+.\d+/g).exec(x).toString();
    var splitted = couple.split(", ");
    return "["+splitted[1]+","+splitted[0]+"]";
                            } );
b.join();
1
  • 4
    Question time and answer time look pretty close for someone who "Cant wait".
    – BradHards
    Commented Jul 2, 2017 at 1:55

Your Answer

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

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