Google Maps Generating Javascript Using Polygons

I am trying to get the Google Maps Javascript API to get points from JSON and use the computeOffset () function to calculate the points left and right so that I can plot a path using polygons. My current code seems to be creating strange shapes instead of making clean polygon connections from point to point.

I define var lastPoint to be lastPoint from a jQuery $ .each loop, so that when the query goes to the next lat lng value, the polygon will connect to the previous point. Instead of nice square polygons from point to point, I get weird shapes, mostly triangles instead of squares.

$.getJSON('sixth.json', function(data) { 
  var lastPoint;
  var myLatlng2;
  var polyShape;

  $.each( data.contactdetails, function(i, value) {
    myLatlng2 =  new google.maps.LatLng(value.lat, value.lng);

    var boomwidth = value.boomwidth;
    var bear = value.bear;
    var boomconversionfactor = 1;

//computeOffset returns a LatLng position by using the bearing and number of feet to return another location
    var point1 = google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2, bear + 90);
    var point2 = google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2, bear - 90);
    var point3 = google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2, 90 + bear);
    var point4 = google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2, bear - 90);

    if (lastPoint !== undefined){
      var point1 = google.maps.geometry.spherical.computeOffset(lastPoint, (0.30480 * boomwidth * boomconversionfactor) / 2, bear + 90);
      var point2 = google.maps.geometry.spherical.computeOffset(lastPoint, (0.30480 * boomwidth * boomconversionfactor) / 2, bear - 90);
    }

    var Coords = [point1,point2, point3, point4];

    // Construct the polygon.
    polyShape = new google.maps.Polygon({
      paths: Coords,
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35
    });

    polyShape.setMap(map); 

//assign lastPoint the lat lng value from the jQuery loop. Trying to connect previous position to next by placing the lastPoint outside the loop.

  lastPoint = myLatlng2;

  }); //close of .each jQuery loop




 });

      

This is what the json contains. When I use an alert or a graph with markers, the locations are shown on the map correctly.

Json

Here's a picture. Instead of purely covering polygons, I end up with these triangular shapes.

Google Map's Image with unclosed Red Polygons

+3


source to share


1 answer


If the bearing is less than 90, then the point to the left for the polygon in the computeOffset () method will not work, because computeOffset () only takes a value of 0-360, starting at 0 and going clockwise from north. So if the bearing is less than 90, just take 360 ​​and add bearing - 90. If the bearing is greater than 270, take bearing + 90 and subtract 360.

The following code will make polygons the correct shape around the path:

   var bear2;
   if(bear>270){bear2 = (bear + 90) - 360;} else{bear2 = bear + 90;}
   var bear3;
   if(bear<90) { bear3 = 360 + (bear -90);} else{bear3 = bear - 90;}


    var point1 =    google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2, bear2);
 var point2 = google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2, bear3);
   var point3 = google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2,  bear2);
   var point4 = google.maps.geometry.spherical.computeOffset(myLatlng2, (0.30480 * boomwidth * boomconversionfactor) / 2, bear3);

        if (lastPoint !== undefined){
  var point1 =    google.maps.geometry.spherical.computeOffset(lastPoint, (0.30480 * boomwidth * boomconversionfactor) / 2 , bear3);
 var point2 = google.maps.geometry.spherical.computeOffset(lastPoint, (0.30480 * boomwidth * boomconversionfactor) / 2, bear2);

        }

      



0.30480 * boomWidth * boomconversionfactor simply sets the width of the polygon in units of feet. The bactoconversion factor can be set to 1 (so it remains feet) or 3.28 (for counters) using the boolean and if operator. Splitting by two is used because the width is determined by the distance left and right.

The polygons now look like this:

Polygons formed around path

+6


source







All Articles