Hello,

I have put together some code to calculate distances between two zip
codes.
When I enter the zip codes through the api I get a different distance
than I do with the actual google maps site.
Anyone have an idea on why this is occurring?

A few ideas I have are:
I am not centering to the zip code -> I was looking around to see how
I could do this but no luck.
The Google maps site is including additional options that I am missing
Issue with United Kingdom zipcodes -> read an old article stating that
the UK has a strict requirement, not sure about this one.

The values I am testing are:
Origin: le17 6qt United Kingdom
Destination: Le16 9ul United Kingdom

Here is my code:
http://www.pastie.org/883475

<script type="text/javascript">

    var map;
    var directionsService = new google.maps.DirectionsService();
    var geocoder = new google.maps.Geocoder();
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var calcDist;

    function initialize(address) {
        if (geocoder) {
            geocoder.geocode({ 'address': address }, function
(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                   var myOptions = {
                        zoom: 13,
                        center: results[0].geometry.location,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    map = new
google.maps.Map(document.getElementById("map"), myOptions);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                } else {
                    alert("Geocode was not successful for the
following reason: " + status);
                }
            });
        }
    }


    function calcMileage(distType, start, end, fieldName) {

        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById("dir"));

        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,

        };

        //send request
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {

                directionsDisplay.setDirections(response);

                //This example only uses one trip and one route
                var myRoute = response.trips[0].routes[0];

                //Gives you distance in meters
                var myRouteMeters = myRoute.distance.value;

                //Gives you text representation using units of origin
country (34.5 mi)
                var myRouteOrigin = myRoute.distance.text;

                //alert(myRouteOrigin);

                //Removing "mi" from mileage
                var myRouteMiles = myRouteOrigin.split(' ');
                calcDist = myRouteMiles[0];

            }
            else {
                calcDist = 0;
            }

            $('#mileage').val(myRouteMeters * 0.000621371192);
            $('#kilometers').val(myRouteMeters * 0.001);
        });

    }

    $(document).ready(function() {
        initialize('60545 United States');

        $('#getMileage').click(function() {
            var postalcode1 = $('#postalcode1').val();
            var country1 = $('#country1').val();
            var postalcode2 = $('#postalcode2').val();
            var country2 = $('#country2').val();

            calcMileage('mi', postalcode1 + ' ' + country1,
postalcode2 + ' ' + country2, 'miDistance');
            return false;
        });
    });

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to