JavaScript:谷歌地图地图选项



我正在尝试弄清楚如何在代码中的谷歌地图中将KM转换为英里数。

基本上,我

使用Google地图显示2个位置之间的距离和时间,一切正常,但是我遇到的问题是它以KM而不是英里为单位显示距离。

是否有任何额外的地图选项可以用来获取以英里为单位的距离?

这是我的整个代码:

http://jsfiddle.net/1on2yumf/1/

问题是它在 JSFIDDLE 中以英里为单位显示距离,但它在我的页面中以公里为单位显示距离,这是我经历过的最奇怪的事情!

我的代码与上面在JSFIDDLE中显示的代码相同。

整个代码:

<script>
    (function () {

        var directionsService = new google.maps.DirectionsService(),
            directionsDisplay = new google.maps.DirectionsRenderer(),
            createMap = function (start) {
                var travel = {
                        origin : (start.coords)? new google.maps.LatLng(start.lat, start.lng) : start.address,
                        destination : "<?php echo $CUpostCode; ?>",
                        travelMode : google.maps.DirectionsTravelMode.DRIVING
                        // Exchanging DRIVING to WALKING above can prove quite amusing :-)
                    },

                    mapOptions = {
                        zoom: 2,
                        // Default view: downtown Stockholm
                        center : new google.maps.LatLng(59.3325215, 18.0643818),
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                          };
                map = new google.maps.Map(document.getElementById("map"), mapOptions);
                directionsDisplay.setMap(map);
                directionsDisplay.setPanel(document.getElementById("map-directions"));
                directionsService.route(travel, function(result, status) {
                    if (status === google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(result);
                    }
                });
            };
            // Check for geolocation support    
            if (navigator.geolocation) {

                window.onload = (function (position) {
                        // Success!
                        createMap({
                            coords : true,
                            //lat : position.coords.latitude,
                            //lng : position.coords.longitude

                            lat : <?php echo $curLat; ?>,
                            lng : <?php echo $curLon; ?>
                        });
                    }, 
                    function () {
                        // Gelocation fallback: Defaults to Stockholm, Sweden
                        createMap({
                            coords : true,
                            //lat : position.coords.latitude,
                            //lng : position.coords.longitude

                            lat : <?php echo $curLat; ?>,
                            lng : <?php echo $curLon; ?>
                        });
                    }
                );
            }
            else {
                // No geolocation fallback: Defaults to Lisbon, Portugal
                createMap({
                            coords : true,
                            //lat : position.coords.latitude,
                            //lng : position.coords.longitude

                            lat : <?php echo $curLat; ?>,
                            lng : <?php echo $curLon; ?>
                });


            }
    })();
</script> 

在标题中:

<script src="http://maps.google.se/maps/api/js?sensor=false"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

任何建议将不胜感激。

我建议明确定义指标

只需在此代码中添加一行unitSystem: google.maps.UnitSystem.METRIC,如下所示:

var travel = {
               origin : (start.coords)? new google.maps.LatLng(start.lat, start.lng) : start.address,
               destination : "<?php echo $CUpostCode; ?>",
               travelMode : google.maps.DirectionsTravelMode.DRIVING,
               unitSystem: google.maps.UnitSystem.IMPERIAL
               // Exchanging DRIVING to WALKING above can prove quite amusing :-)
},

最新更新