简体   繁体   English

在Google地图中的点之间绘制方向

[英]Draw the Direction between points in google maps

We have json like this 我们有这样的json

var myjson=[ { "Longitude": "17.4266008", "Lattitude": "78.5497557", "UserID": "2" }, { "Longitude": "17.4265974", "Lattitude": "78.5497276", "UserID": "2" }, { "Longitude": "17.4266509", "Lattitude": "78.5496451", "UserID": "2 " }, { "Longitude": "17.4266506", "Lattitude": "78.5497198", "UserID": "2" }, { "Longitude": "17.4266805", "Lattitude": "78.5496888", "UserID": "2" }, { "Longitude": "17.4266805", "Lattitude": "78.5496888", "UserID": "2" }, { "Longitude": "17.4266805", "Lattitude": "78.5496888", "UserID": "2" }, ];

We need Like this 我们需要这样 在此处输入图片说明 So We tried like this 所以我们尝试这样

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

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
    zoom: 6,
    center: chicago
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);
}

function calcRoute() {
  var start = document.getElementById('start').value;
  var end = document.getElementById('end').value;
  var waypts = [];
  var checkboxArray = document.getElementById('waypoints');
  for (var i = 0; i < checkboxArray.length; i++) {
    if (checkboxArray.options[i].selected == true) {
      waypts.push({
          location:checkboxArray[i].value,
          stopover:true});
    }
  }

  var request = {
      origin: start,
      destination: end,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      var route = response.routes[0];
      var summaryPanel = document.getElementById('directions_panel');
      summaryPanel.innerHTML = '';
      // For each route, display summary information.
      for (var i = 0; i < route.legs.length; i++) {
        var routeSegment = i + 1;
        summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
        summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
        summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
        summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
      }
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

We know only draw Lines Between two points but We have json.In json have number of latitude and longitude .We need to Draw Lines Between points.Please guide me.And tell me what wrong in my code. 我们只知道在两点之间画线,但是我们有json。在json中有纬度和经度数。我们需要在点之间画线。请指导我,并告诉我代码中有什么错误。 calcRoute method is called in button action 在按钮操作中调用calcRoute方法

Instead of reading the waypoints from the DOM, read them from your array. 与其从DOM中读取航路点,不如从数组中读取它们。 Something like 就像是

function calcRoute() {
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;
    var waypts = [];

    var myjson = [{
        "Longitude": "17.4266008",
        "Lattitude": "78.5497557",
        "UserID": "2"
    }, {
        "Longitude": "17.4265974",
        "Lattitude": "78.5497276",
        "UserID": "2"
    }, {
        "Longitude": "17.4266509",
        "Lattitude": "78.5496451",
        "UserID": "2 "
    }, {
        "Longitude": "17.4266506",
        "Lattitude": "78.5497198",
        "UserID": "2"
    }, {
        "Longitude": "17.4266805",
        "Lattitude": "78.5496888",
        "UserID": "2"
    }, {
        "Longitude": "17.4266805",
        "Lattitude": "78.5496888",
        "UserID": "2"
    }, {
        "Longitude": "17.4266805",
        "Lattitude": "78.5496888",
        "UserID": "2"
    }];


    for (var i = 0; i < myjson.length; i++) {
        waypts.push({
            location: new google.maps.LatLng(parseFloat(myjson[i].Lattitude), parseFloat(myjson[i].Longitude)),
            stopover: true
        });
    }

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

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