简体   繁体   English

Google Maps指示mvcobject获得单个步骤

[英]Google Maps directions mvcobject getting a individual step

I am using directionsService to get Directions via the google maps javascript api. 我正在使用DirectionsService通过Google Maps JavaScript API获取路线。 The issue is that I want to get individual steps and yet have them be part of the mvc object so when you click on them they render on the map as the default 问题是我想获得单个步骤,但仍将它们作为mvc对象的一部分,因此当您单击它们时,它们将作为默认值呈现在地图上

directionsDisplay.setDirections(response); 

does. 做。 Some Code: 一些代码:

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

var request = {
        origin : a,
        destination : b,
        travelMode : google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {

    if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
          //Instead of setting directions to a panel i want to 
          //set just one to a panel and yet have it link to the
          //map like the above does.
            }});

The idea is you must iterate over each leg of the route, then over each step in the leg and render your own HTML to represent it. 这个想法是,您必须遍历路线的每个分支,然后遍历该分支的每个步骤,并呈现自己的HTML来表示它。 See the following code (assumes you have map , directionsDisplay , and directionsService , and a request defined): 请参见以下代码(假设您具有mapdirectionsDisplaydirectionsService ,并定义了一个request ):

directionsService.route(request, function(response, status) {
  // Make sure the directions request was valid
  if (status == google.maps.DirectionsStatus.OK) {
    // Give the response to the directions display
    directionsDisplay.setDirections(response);

    // Display the route on the map
    directionsDisplay.setMap(map);

    // Grab the result from the routes
    var routeResult = response.routes[0];

    // Iterate over each leg of the route
    routeResult.legs.forEach(function(leg) {
      // Build HTML for each step
      var stepHTML = '';
      leg.steps.forEach(function(step) {
        stepHTML += '<div class="directionStep">'+
                  '<div>'+step.instructions+'</div>'+
                  '<div class="grey">'+step.distance.text+' ('+step.duration.text+')'+'</div>'+
                '</div>';
      });

      // Put the step HTML somewhere
      $('.steps').append(stepHTML);
    });
  }
});

Then, listen to clicks on the individual steps and do whatever you need to in response. 然后,听每个步骤的点击,并做您需要做的一切作为回应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM