Search code examples
google-mapswt

Issue with lat and long accuracy, depending on the places


I'm asking you because I struggled for a few days now ; I searched the Internet and stackoverflow and didn't find anything.

I need to place markers/circles/whatever quite precisely on a map. However, it seems that I can only draw them on a grid whose degree of precision depends on the place I am. Therefore, their location is not accurate enough.

I'm using Wt (C++), but it doesn't seem to be part of the issue. I wrote code that draws a 20*20 array of circles, whose coordinates are equally spaced. I don't get the same results whether I'm in Paris or in New York.

In Paris : (can't post images, because it's my first question) http://img818.imageshack.us/img818/7841/parism.png

In New-York : http://img193.imageshack.us/img193/9822/newyorkq.png

In Paris, I get accuracy in longitude, but not in latitude. In New-York, I can't get accuracy either in latitude nor in longitude.

Here is the code I used :

    /*Choose between both*/
    double centerLat = 40.714468; double centerLng = -74.005966;//New-York
    //double centerLat = 48.854607; double centerLng = 2.347126;//Paris

    /*Other parameters*/
    double lngMargin = 0.000400;
    double latMargin = 0.000400;
    int granularity = 20;

    int i,j;
    WVBoxLayout* layout = new WVBoxLayout();
    WColor::WColor myBlue = WColor::WColor(0,0,255,255);
    WColor::WColor myRed  = WColor::WColor(255,0,0,255);
    WColor::WColor myTransparent = WColor::WColor(0,0,0,0);
    WGoogleMap::Coordinate coordArray[granularity][granularity];

    /* Creating and configuring the map */
    WGoogleMap *map = new WGoogleMap(WGoogleMap::Version3);
    WGoogleMap::Coordinate centerCoord = WGoogleMap::Coordinate(centerLat,centerLng);
    setLayout(layout);
    resize(height,width);
    map->setCenter(centerCoord,19);
    map->setMapTypeControl(WGoogleMap::DefaultControl);
    map->enableScrollWheelZoom();
    map->addCircle(centerCoord,2,myBlue,2,myTransparent);

    /* Here is the loop that draws the circles */
    for(i=0 ; i<=granularity-1 ; i++){
            for(j=0 ; j<=granularity-1 ; j++){

                    coordArray[i][j] = WGoogleMap::Coordinate(centerLat + beforeOrAfterTheCenter_Lat * (latMargin/granularity) * i,
                                    centerLng + beforeOrAfterTheCenter_Lng * (lngMargin/granularity) * j); 

                    map->addCircle(coordArray[i][j],1,myRed,2,myTransparent);
            }
    }

The code is supposed to work okay. Do you have any clue about what I could do to get more accuracy ? Is it a well-known issue ?

Thank you very much for any help you could provide,

L.


Solution

  • After some hours of struggling, I finally found out what the issue was.

    I'm using Wt (C++), but it doesn't seem to be part of the issue

    Well, it was actually. Wt uses a std::stringstream to generate JavaScript code, and this object doesn't has default infinite precision when you give him a double : it just takes 6 digits. That means that if you have 1 digit before the point, you have 5 after, and if you have 2 digits before the point, you have 4 after. That's why we had great precision for longitude in Paris, but not for latitude (lng is 6 but lat is 45). And that's why precision for both latitude and longitude in New-York was poor (both have 2 digits before the point)

    To solve the issue, I just overloaded the addCircle (and addMarker and all the other functions) with a custom function of mine and I set the precision of the std::stringstream used with the std::stringstream::precision(int n) method that I didn't know yet.

    I'm reporting this issue on the Wt redmine, it may be corrected soon.

    Heitor, muito obrigado pela ajuda.

    See you,