问题
The google maps v3 directionsService returns lat/long points that are sometimes longer than I want. Here is a call I might make:
var request = {
origin: start_position,
destination: end_position,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
// do something with response, e.g. set the overview_path to a polyline;
}
});
The resulting response might return the following values for a point:
response.routes[0].legs[0].start_location.Xa = 35.077000000000005
response.routes[0].legs[0].start_location.Ya = -85.08854000000001
I really want to lessen the precision. For example I would prefer values to a precision of five decimal places like this:
Xa = 35.07700
Ya = -85.08854
Is there an easy way to accomplish this? I have looked through the documentation and I don't see any mention of a way to set this.
Thanks for any insight or help you might offer!
回答1:
First of all you shouldn't use .Xa and .Ya because those are minified names that can change with the next release of the API. Instead you should use the documented methods lat() and lng(). so:
response.routes[0].legs[0].start_location.lat()= 35.077000000000005
response.routes[0].legs[0].start_location.lng() = -85.08854000000001
and if you want to round the decimals then you'd use the standard javascript method .toFixed(), that is:
var lat = response.routes[0].legs[0].start_location.lat().toFixed(5);// 35.07700
var lon = response.routes[0].legs[0].start_location.lng().toFixed(5);// -85.08854
回答2:
Javascript uses double precision floating point numbers to represent data. If you are saving that data as a string, it might make sense to output it using .toFixed(); but that would be your code, not Google's.
来源:https://stackoverflow.com/questions/12208840/how-to-set-precision-of-results-returned-by-google-maps-v3-api-directionsservice