在Google Maps Distance Service API中路由URL

时间:2018-03-26 17:18:06

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

我打电话从谷歌地图远程服务API获取多条路线,我能够获得成功的回复。但我想获取路线网址(shortURL),因此如果客户点击链接,他将直接重定向到显示路线的谷歌地图。这是我的代码:

var directionsService = new google.maps.DirectionsService;
    return directionsService.route({
        origin: origin,
        destination: destination,
        travelMode: 'DRIVING',
        provideRouteAlternatives: true,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
    }, shortestRoute);

1 个答案:

答案 0 :(得分:1)

如果您没有尝试使用Google地图路线服务进行导航,那么您可以做的是根据用户输入动态提供(shortURL),假设您正在收听应用程序一方的用户输入。 / p>

一个简单的实现是,您可以立即提供(shortURL),以便用户可以在实际的Google Maps page上输入路线信息,而不是在您的网站上输入,前提是您没有加载模态和/或者iframe,因为你提到了重定向。

现在,如果您在用户输入数据之前没有计划重定向。你可以设置类似的东西:

Documentation

var origin;

// This function gets called when you are ready to submit the user's input. 
function directionsOrigin(){ 
  var origin = document.getElementById('origin-input').value;
  placeholdDynamicInput(origin);
}

function placeholdDynamicInput(origin, dynamicUrl){
  // Following this
  // protocol-> https://www.google.com/maps/dir/?api=1&parameters

  var url = 'https://www.google.com/maps/dir/?api=1&origin=' + origin;

  // Dynamically create your placeholder URL using user input.
}

function dynamicUrl(){
  // Handle this function however makes sense with your application
}