方向服务Google API,可拖动来源

时间:2019-01-16 08:01:43

标签: javascript google-maps

首先,我将键入起点和终点,并显示从起点A到终点B的显示路线。但是,我想使起点A可拖动,以便重新计算路线并向我显示。

 var directionsDisplay = new google.maps.DirectionsRenderer;
    var directionsService = new google.maps.DirectionsService;
    var map = new google.maps.Map(document.getElementById('map'), {
      mapTypeControl: false,
      center: {lat: 1.317206, lng: 103.772240},
      zoom: 13
    });

    new AutocompleteDirectionsHandler(map);
     directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('right-panel'));

            var onChangeHandler = function() {
      calculateAndDisplayRoute(directionsService, directionsDisplay);
    };

    document.getElementById('destination-input').addEventListener('change', 
 onChangeHandler);
 }




  function calculateAndDisplayRoute(directionsService, 
 directionsDisplay) {
    var start = document.getElementById('origin-input').value;
    var end = document.getElementById('destination-input').value;


    directionsService.route({
      origin: start,

      destination: end,
      travelMode: 'DRIVING'
    }, function(response, status) {
      if (status === 'OK') {
        directionsDisplay.setDirections(response);
      }
    });

   }

下面提供了HTML代码。

<body>

<input id="origin-input" class="controls" type="text" 
    placeholder="Enter an origin location">

<input id="destination-input" class="controls" type="text"
    placeholder="Enter a destination location">

    <div id="map"></div>
</body>

下面是我当前项目的屏幕截图。 Screenshot

1 个答案:

答案 0 :(得分:0)

请尝试:

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);

//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);

//add button to the layout
layout.addView(btnTag);