gpt4 book ai didi

google-maps-api-3 - Google Maps v3在两点之间创建路线

转载 作者:行者123 更新时间:2023-12-04 13:23:38 25 4
gpt4 key购买 nike

我正在使用Google Maps API开发网络应用程序。我试图在两点之间创建一条路线,但是由于某种原因,我还没有弄清楚如何创建它。以下是我的代码,请让我知道是否缺少某些内容。谢谢你。

<script>
var Center=new google.maps.LatLng(18.210885,-67.140884);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var properties = {
center:Center,
zoom:20,
mapTypeId:google.maps.MapTypeId.SATELLITE
};

map=new google.maps.Map(document.getElementById("map"), properties);
directionsDisplay.setMap(map);

var marker=new google.maps.Marker({
position:Center,
animation:google.maps.Animation.BOUNCE,
});

marker.setMap(map);

}

function Route() {

var start = new google.maps.LatLng(18.210885,-67.140884);
var end =new google.maps.latLng(18.211685,-67.141684);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}

google.maps.event.addDomListener(window,'load',initialize);
</script>

最佳答案

如果我执行以下操作,它将对我有效:

  • 调用Route函数
  • 更改:
    var end =new google.maps.latLng(18.211685,-67.141684);

  • 到:
        var end =new google.maps.LatLng(18.211685,-67.141684);

    (JavaScript区分大小写,浏览器在javascript控制台中报告了错误)

    working version

    代码段:

    var Center = new google.maps.LatLng(18.210885, -67.140884);
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;

    function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var properties = {
    center: Center,
    zoom: 20,
    mapTypeId: google.maps.MapTypeId.SATELLITE
    };

    map = new google.maps.Map(document.getElementById("map"), properties);
    directionsDisplay.setMap(map);

    var marker = new google.maps.Marker({
    position: Center,
    animation: google.maps.Animation.BOUNCE,
    });

    marker.setMap(map);
    Route();
    }

    function Route() {

    var start = new google.maps.LatLng(18.210885, -67.140884);
    var end = new google.maps.LatLng(18.211685, -67.141684);
    var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.WALKING
    };
    directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(result);
    } else {
    alert("couldn't get directions:" + status);
    }
    });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map {
    margin: 0;
    padding: 0;
    height: 100%;
    }
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <div id="map"></div>

    关于google-maps-api-3 - Google Maps v3在两点之间创建路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14556312/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com