【问题标题】:Directions Service of Google Maps API doesn't workGoogle Maps API 的路线服务不起作用
【发布时间】:2014-05-01 16:40:27
【问题描述】:

您知道为什么我的 Google Maps API 路线服务不起作用吗?似乎没有执行方法 commandsService.route() (导致未显示状态警报),但我不知道为什么。我是 Google Maps API 和 JS 的新手,所以如果它很简单,请尽量宽容。 :)

var map = null;
var pos = null;
const STORED_LOC = new google.maps.LatLng(50.405196, 11.894855);
var currentLat = document.getElementById("latLabel");
var currentLng = document.getElementById("lngLabel");
var additionalInfo = document.getElementById("additionalInfo");

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

function initialize()
{
    pos = STORED_LOC;
    currentLat.innerHTML = pos.lat();
    currentLng.innerHTML = pos.lng();

    options =
        {
            center: pos,
            zoom: 15
        }

    marker = new google.maps.Marker(
        {
            position: pos,
            map: map,
            title: "Chosen localization"
        }
    );

    map = new google.maps.Map(document.getElementById("mapContainer"), options);
    marker.setMap(map);

    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(map);
}

$("#yes").click(function () {
    getPosition();
    hideUserConsentSection();
});

$("#no").click(function () {
    hideUserConsentSection();
    showSetCustomLocationSection();
});

function showSetCustomLocationSection() {
    $("#setCustomLocationSection").show();
}

function hideUserConsentSection() {
    $("#userConsentSection").hide();
}

function getPosition() {

    if (navigator.geolocation) {
        var options = {
            enableHighAccuracy: true,
            timeout: 20000,
            maximumAge: 2000
        }

        navigator.geolocation.getCurrentPosition(showPosition, errorPosition, options);
    }

    else
    {
        additionalInfo.innerHTML += "Your browser doesn't support geolocation";
    }
}

function showPosition(position) {

    pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    currentLat.innerHTML = pos.lat();
    currentLng.innerHTML = pos.lng();

    var request =
        {
            origin: STORED_LOC,
            destination: pos,
            travelmode: google.maps.TravelMode.DRIVING
        }
    directionsService.route(request, function(result, status)
    {
        alert(status);
        if (status == google.maps.DirectionsStatus.OK)
        {
            alert("OKAY");
            directionsDisplay.setDirections(result);
        }
    });
    //map = new google.maps.Map(document.getElementById("mapContainer"), options);
    //marker.setMap(map);

}

function errorPosition(position) {
    switch (position.code) {
        case 1:
            showSetCustomLocationSection();
            break;
        case 2:
            showSetCustomLocationSection();
            break;
        case 3:
            showSetCustomLocationSection();
            break;
        default:
            break;
    }
}

google.maps.event.addDomListener(window, 'load', initialize);

我的 HTML 看起来像这样:

<h3>How to reach us?</h3>

<div id="userConsentSection">Can we use your geolocation?<br />
     <input type="button" id="yes" value="Yes" />
    <input type="button" id="no" value="No" /><br /><br />
</div>

<div id="setCustomLocationSection" style="display:none">
    Enter your geolocation. <br /><br />
    <input type="text" id="customLocation" />
    <input type="button" id="setCustomLocationButton" value="Show" /><br /><br />
</div>

<span>Latitude: </span>
<div id="latLabel">
</div>

<span>Longitude: </span>
<div id="lngLabel">
</div>

<div id="additionalInfo">
</div>

<div id="mapContainer" style="height: 400px; width: 100%">

</div>

<script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?key=XXXX&sensor=true">
</script>

【问题讨论】:

  • 您是否在控制台中看到任何错误消息?
  • 你的 HTML 是什么样的?重现问题的 jsfiddle 会很有用。
  • 对于一个新手,你有很多代码。从Demo开始并理解它。然后逐步添加功能,直到完成所需的产品
  • @davidstrachan - 整个代码对我来说是可以理解的,我写的,这就是为什么我不知道我犯了什么错误。您的帖子根本没有用,而且完全没有生产力。如果您知道那段代码有什么问题 - 请解释一下。
  • 您可能想要更改您的标题。考虑到成千上万个网站(如果不是数百万个网站)成功使用 API,那么问题很有可能不是 api 不起作用

标签: javascript google-maps google-maps-api-3


【解决方案1】:

答案是请求对象中的拼写错误 - travelmode 而不是 travelMode。此参数是必需的,因此不会执行路由方法。小心那个名字!

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多