mukshs / google-maps-utility-library-v3

Automatically exported from code.google.com/p/google-maps-utility-library-v3
Apache License 2.0
0 stars 0 forks source link

RouteBoxer bug #274

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
RouteBoxer with this input points fails:
<html>
<head>
</head>
<body>
</body>
<script src="http://maps.google.com/maps/api/js?sensor=false" 
type="text/javascript" style=""></script>
<script type="text/javascript" 
src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/routeboxer/s
rc/RouteBoxer.js"></script>
<script type="text/javascript">
var p1 =new google.maps.LatLng(6,41);
var p2 =new google.maps.LatLng(5,41);
var res= (new RouteBoxer()).box([p1,p2],1);
console.log(res);
</script>
<html>

RouteBoxer fails because points have the same longitude.
To resolve the bug I have updated the method buildGrid in this way:
RouteBoxer.prototype.buildGrid_ = function (vertices, range) {

  // Create a LatLngBounds object that contains the whole path
  var routeBounds = new google.maps.LatLngBounds();
  for (var i = 0; i < vertices.length; i++) {
    routeBounds.extend(vertices[i]);
  }

  // Find the center of the bounding box of the path
  var routeBoundsCenter = routeBounds.getCenter();

  // Starting from the center define grid lines outwards vertically until they
  //  extend beyond the edge of the bounding box by more than one cell
  this.latGrid_.push(routeBoundsCenter.lat());

  // Add lines from the center out to the north
  this.latGrid_.push(routeBoundsCenter.rhumbDestinationPoint(0, range).lat());
  for (i = 2; this.latGrid_[i - 2] < routeBounds.getNorthEast().lat(); i++) {
    this.latGrid_.push(routeBoundsCenter.rhumbDestinationPoint(0, range * i).lat());
  }
  this.latGrid_.push(routeBoundsCenter.rhumbDestinationPoint(0, range * i).lat());

  // Add lines from the center out to the south  
  for (i = 1; this.latGrid_[1] > routeBounds.getSouthWest().lat(); i++) {
    this.latGrid_.unshift(routeBoundsCenter.rhumbDestinationPoint(180, range * i).lat());
  }
  this.latGrid_.unshift(routeBoundsCenter.rhumbDestinationPoint(180, range * i).lat());

  // Starting from the center define grid lines outwards horizontally until they
  //  extend beyond the edge of the bounding box by more than one cell  
  this.lngGrid_.push(routeBoundsCenter.lng());

  // Add lines from the center out to the east
  this.lngGrid_.push(routeBoundsCenter.rhumbDestinationPoint(90, range).lng());
  for (i = 2; this.lngGrid_[i - 2] < routeBounds.getNorthEast().lng(); i++) {
    this.lngGrid_.push(routeBoundsCenter.rhumbDestinationPoint(90, range * i).lng());
  }
  this.lngGrid_.push(routeBoundsCenter.rhumbDestinationPoint(90, range * i).lng());

  // Add lines from the center out to the west
  for (i = 1; this.lngGrid_[1] > routeBounds.getSouthWest().lng(); i++) {
    this.lngGrid_.unshift(routeBoundsCenter.rhumbDestinationPoint(270, range * i).lng());
  }
  this.lngGrid_.unshift(routeBoundsCenter.rhumbDestinationPoint(270, range * i).lng());

  // Create a two dimensional array representing this grid
  this.grid_ = new Array(this.lngGrid_.length);
  for (i = 0; i < this.grid_.length; i++) {
    this.grid_[i] = new Array(this.latGrid_.length);
  }
};

Original issue reported on code.google.com by marcobia...@gmail.com on 12 Oct 2013 at 2:07