1

我有一个项目需要为两点之间的路线设置多种颜色。我已经使用 Google maps API 来使代码正常工作。这是我正在处理的代码。

HTML部分:

<!DOCTYPE html>  
 <html>  
  <head>  
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">  
<meta charset="utf-8">  
<style type="text/css">  
  html { height: 100% }  
  body { height: 100%; margin: 0; padding: 0 }  
  #map-canvas { height: 100% }  
</style>  
<title>Waypoints in directions</title>  
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>          </head>  
<body>  
  <div id="map-canvas" style="float:left;width:70%;height:100%;"></div>  
  <div id="control_panel" style="float:right;width:30%;text-align:left;padding-top:20px">  
  <div style="margin:20px;border-width:2px;">  
    <div id="panel">  
    <b>Start: </b>  
  <input id="start" type="text" onchange="calcRoute();">  
  <br>  
  <b>End: </b>  
  <input id="end" type="text" onchange="calcRoute();">  
  </div>  
  <br>  
  <b>Waypoints:</b> <br>  
  <p><input type="button" value="waypoint" onclick="waypoint();" width="100px"   height="60px"></p>  
  </div>
  <div id="div"></div>
  <p><input type="button" value="submit" onclick="calcRoute();" width="100px" height="60px"></p>
  </div>  
</body>  
</html>  

JavaScript 部分:

function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer({
polylineOptions: {
  strokeColor: "blue"
}
  });   
directionsDisplay1 = new google.maps.DirectionsRenderer({
 suppressMarkers: true,
   polylineOptions: {
     strokeColor: "green"
  }
     });
 directionsDisplay2 = new google.maps.DirectionsRenderer({
  suppressMarkers: true,
    polylineOptions: {
     strokeColor: "orange"
     }
   });
 directionsDisplay3 = new google.maps.DirectionsRenderer({
    suppressMarkers: true,
    polylineOptions: {
      strokeColor: "red"
}
  });

  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
   }
   map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
   directionsDisplay.setMap(map);
   directionsDisplay1.setMap(map);
   directionsDisplay2.setMap(map);
   directionsDisplay3.setMap(map);

}
function calcRoute() {
  start = document.getElementById('start').value;
  end = document.getElementById('end').value;


  for (var i = 0; i < counter; i++) {
   checkpts[i] = document.getElementsByClassName('way')[i];
   waypoints.push({
   location:checkpts[i].value,
   stopover:true});
}
  var request = {
  origin: start,
  destination: end,
  waypoints: waypoints,
  optimizeWaypoints: true,
  travelMode: google.maps.TravelMode.DRIVING
};
  directionsService.route(request, function(response, status,routes) {
   if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay.setDirections(response);
  myRoute = response.routes[0];
  }
  for (var i=0; i<myRoute.legs[0].steps.length; i++) {

      pts[i]=myRoute.legs[0].steps[i].polyline.points;
      points[i]=String(pts[i]);
      waypts[i]=decode(points[i]);
      Points.push(waypts[i]);
  }
for (var i=0; i<myRoute.legs[0].steps.length; i++){
   for (var j=0; j<Points[i].length; j++){
    routepts.push(Points[i][j]);
  }
}

  var s=routepts.length;
  var k=(s/4);
 for(var i=1;i<=4;i++)
 {
      latlng[i-1] = new google.maps.LatLng(routepts[(parseInt(i*k)-1)][0],       routepts[(parseInt(i*k)-1)][1]);

 }
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({'latLng': latlng[0]},function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
             address.push(results[1].formatted_address);
           }
          }
      });
   geocoder.geocode({'latLng': latlng[1]},function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
             address.push(results[1].formatted_address);
            }
      }
  });
   geocoder.geocode({'latLng': latlng[2]},function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
          address.push(results[1].formatted_address);
        }
      }
  });
  geocoder.geocode({'latLng': latlng[3]}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
          address.push(results[1].formatted_address);
        }
      }
  });
})
 var request1 = {
  origin: start,
  destination: String(address[1]),
  waypoints: waypoints,
  optimizeWaypoints: true,
  travelMode: google.maps.TravelMode.DRIVING
  };

directionsService.route(request1, function(response, status,routes) {
   if (status == google.maps.DirectionsStatus.OK) {
     directionsDisplay1.setDirections(response);
    }
   })

 var request2 = {
  origin: String(address[1]),
  destination: String(address[2]),
  waypoints: waypoints,
  optimizeWaypoints: true,
  travelMode: google.maps.TravelMode.DRIVING
  };

directionsService.route(request2, function(response, status,routes) {
if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay2.setDirections(response);
}
  })

var request3 = {
  origin: String(address[2]),
  destination: end,
  waypoints: waypoints,
  optimizeWaypoints: true,
  travelMode: google.maps.TravelMode.DRIVING
  };

directionsService.route(request3, function(response, status,routes) {
if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay3.setDirections(response);
}
  })


}
function decode(instring) {
  var out=[];
  //instring = instring.replace(/\\\\/g, "\\");
  out = decodeLine(instring);
  return out;
}
function decodeLine(encoded) {
 var len = encoded.length;
 var index = 0;
 var array = [];
 var lat = 0;
 var lng = 0;

  while (index < len) {
   var b;
   var shift = 0;
var result = 0;
do {
  b = encoded.charCodeAt(index++) - 63;
  result |= (b & 0x1f) << shift;
  shift += 5;
} while (b >= 0x20);
var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;

shift = 0;
result = 0;
do {
  b = encoded.charCodeAt(index++) - 63;
  result |= (b & 0x1f) << shift;
  shift += 5;
} while (b >= 0x20);
var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;

array.push([lat * 1e-5, lng * 1e-5]);
  }

  return array;
}

function waypoint(){
  var d=document.getElementById("div");
  d.innerHTML+="<p><input type='text' placeholder='waypoint' class='way'>";
  counter++;

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

运行代码时,出现错误。

  1. 在开始或结束输入框中输入位置时,将在输入的位置和西班牙的未知位置之间绘制路线。

我是 Google Maps API 的新手。请提出一些帮助我的建议。
提前致谢。

4

0 回答 0