html 在Google Directions路线中绘制折线

im9ewurl  于 2022-11-20  发布在  Go
关注(0)|答案(1)|浏览(114)

所以,基本上我用谷歌Map使用方向创建了一条路线。我如何解码这个overview_polyline?

function calcRoute() {
  var waypts = [
    { location: "Sao José dos campos,SP", stopover: true },
  ];
  directionsService
    .route({
      origin: "Rio de Janeiro, RJ",
      destination: "São paulo, SP",
      waypoints: waypts,
      travelMode: google.maps.TravelMode.DRIVING,
      provideRouteAlternatives: true,
    })
    .then((response) => {
      console.log({ response });
      directionsRenderer.setDirections(response);
      var rotaFeita = response.routes[0].overview_polyline; // I NEED TO DECODE THIS "rotaFeita"
      google.maps.geometry.decodePath(rotaFeita) 
      console.log(rotaFeita);
    })
    .catch((err) => {
      console.log({ err });
    });
}
rmbxnbpk

rmbxnbpk1#

用你的密码我得到一条信息:TypeError: google.maps.geometry.decodePath is not a function .
.decodePath的正确路径为:google.maps.geometry.encoding.decodePath
要使用该调用的结果创建Polyline,请执行以下操作:

new google.maps.Polyline({
  path: google.maps.geometry.encoding.decodePath(rotaFeita),
  map: map,
  strokeColor: "black",
  strokeOpacity: 1.0,
  strokeWeight: 1
});

proof of concept fiddle

程式码片段:

第一个

相关问题