wrong decimals in google.maps.LatLng ?

2,177 views
Skip to first unread message

bgacias

unread,
Feb 17, 2011, 8:38:55 AM2/17/11
to Google Maps JavaScript API v3
I'm loading the Google Maps JavaScript V3 using:

script.src = "http://maps.google.com/maps/api/js?
v=3&sensor=false&region=ES";



in my javaScript I have these lines:

var lat = parseFloat("39.5775");
var lng = parseFloat("2.714105");
alert("parseLatLng" + new google.maps.LatLng(lat, lng));
alert("parseLatLng with 10 decimals " + new
google.maps.LatLng(lat, lng).toUrlValue(10));

the firs alert shows :
parseLatLng(39.5775, 2.7141050000000178)

and the second shows:
parseLatLng with 10 decimals 39.5775,2.714105

Why the longitude of the LatLng object is 2.7141050000000178 and not
2.714105?

Thanks in advance

Rossko

unread,
Feb 17, 2011, 3:08:11 PM2/17/11
to Google Maps JavaScript API v3
> Why the longitude of the LatLng object is 2.7141050000000178 and not
> 2.714105?

That's how javascript floating point numbers work
http://javascript.about.com/od/hintsandtips/a/Floating-Point-And-Javascript.htm
What issues might this cause for you?
2.7141050000000178 is about 0.000001mm away from 2.714105
The maps really aren't quite that accurate.

Limpinwood

unread,
Feb 17, 2011, 9:25:22 PM2/17/11
to Google Maps JavaScript API v3
Gents hello, I just noticed this happening today, in my case the
original lng coord did not have that degree of precision. It has
worked fine until very recently

Peter

On Feb 18, 7:08 am, Rossko <ros...@culzean.clara.co.uk> wrote:
> > Why the longitude of the LatLng object is 2.7141050000000178 and not
> > 2.714105?
>
> That's how javascript floating point numbers workhttp://javascript.about.com/od/hintsandtips/a/Floating-Point-And-Java...

Chris Broadfoot

unread,
Feb 18, 2011, 12:30:35 AM2/18/11
to google-map...@googlegroups.com, Limpinwood
This is a floating point rounding error introduced by our longitude wrapping function.

If you're absolutely sure that your latitude is between -90 and 90, and longitude between -180 and 180, you could pass true to the noWrap parameter of the LatLng constructor and the wrapping function will be bypassed.

Be wary, though - if your coordinates are outside of these bounds, some API features will break.

Why does this matter? As Rossko says, realistically this is not an issue in terms of error on the earth's surface.

Chris

Limpinwood

unread,
Feb 18, 2011, 12:51:19 AM2/18/11
to Google Maps JavaScript API v3
Thanks Chris,

hopefully that will sort it, it my case it does matter cause we have
multiple records hanging off the marker, I need to get the lat,lng
back from the marker in the same format as provided so I can search
the parent XML for the records at the position.

Thanks again



On Feb 18, 4:30 pm, Chris Broadfoot <c...@google.com> wrote:
> This is a floating point rounding error introduced by our longitude wrapping
> function.
>
> If you're absolutely sure that your latitude is between -90 and 90, and
> longitude between -180 and 180, you could pass true to the noWrap parameter
> of the LatLng constructor and the wrapping function will be bypassed.http://code.google.com/apis/maps/documentation/javascript/reference.h...

Limpinwood

unread,
Feb 18, 2011, 1:00:41 AM2/18/11
to Google Maps JavaScript API v3
Chris, that fixed it, Thanks very much for the prompt action

cheers

Peter

On Feb 18, 4:30 pm, Chris Broadfoot <c...@google.com> wrote:
> This is a floating point rounding error introduced by our longitude wrapping
> function.
>
> If you're absolutely sure that your latitude is between -90 and 90, and
> longitude between -180 and 180, you could pass true to the noWrap parameter
> of the LatLng constructor and the wrapping function will be bypassed.http://code.google.com/apis/maps/documentation/javascript/reference.h...

Chris Broadfoot

unread,
Feb 18, 2011, 1:14:15 AM2/18/11
to google-map...@googlegroups.com, Limpinwood
You shouldn't do this. Attach an identifier to your Marker so you can safely relate it back to your original data source.

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.




--
http://twitter.com/broady

Nikolaj Pedersen

unread,
Feb 18, 2011, 3:29:52 AM2/18/11
to Google Maps JavaScript API v3
Agree.
It is bad news to rely on position values to uniquely identify your
records.
Especially when they are manipulated and converted.
Use an id instead.

.nikolaj


On Feb 18, 7:14 am, Chris Broadfoot <c...@google.com> wrote:
> You shouldn't do this. Attach an identifier to your Marker so you can safely
> relate it back to your original data source.
>

bgacias

unread,
Feb 18, 2011, 3:57:46 AM2/18/11
to Google Maps JavaScript API v3
It happens using the version 3.4 but not if you use the version 3.3

Rossko

unread,
Feb 18, 2011, 4:21:06 AM2/18/11
to Google Maps JavaScript API v3
> hopefully that will sort it, it my case it does matter cause we have
> multiple records hanging off the marker, I need to get the lat,lng
> back from the marker in the same format as provided so I can search
> the parent XML for the records at the position.

Treat this as an opportunity to improve your code and make it more
robust.
As others have said, a fundamental change using a unique ID would be
the usual answer.

But a quick fix is in your reach too - if you "must" have coordinates
rounded to N decimal places, then have your own code round them to N
decimal places.
Reply all
Reply to author
Forward
0 new messages