Google Maps Base

Base for SO questions

by ABBEYSOFT

HTML

<script src="http://maps.google.com/maps/api/js?sensor=false&amp;.js"></script>
<div id="results"></div>
<div id="map"></div>

CSS

#map {
  width: 450px;
  height: 400px;
}

JavaScript

function drawCircle(point, radius, dir, addtoBounds) {
  var extp = []; // best practice is to use [] rather then new Array(), both do the same thing.   
  if (dir == 1) {
    for (var i = 0; i <= 360; i++) {
      //destVincenty function returns a object with lat, lng, and final bearing.     
      var destPoint = destVincenty(point.lat().toRad(), point.lng().toRad(), i.toRad(), radius);

      //add new point 
      extp.push(new google.maps.LatLng(destPoint.lat, destPoint.lng));
      if (addtoBounds) bounds.extend(extp[extp.length - 1]);
    }
  } else {
    for (var i = 360; i => 0; i--) {
      //destVincenty function returns a object with lat, lng, and final bearing.     
      var destPoint = destVincenty(point.lat().toRad(), point.lng().toRad(), i.toRad(), radius);

      //add new point 
      extp.push(new google.maps.LatLng(destPoint.lat, destPoint.lng));
      if (addtoBounds) bounds.extend(extp[extp.length - 1]);
    }
  }

  return extp;
}



/**
 * Calculates destination point given start point lat/long, bearing & distance, 
 * using Vincenty inverse formula for ellipsoids
 *
 * @param   {Number} lat1, lon1: first point in decimal degrees
 * @param   {Number} brng: initial bearing in decimal degrees
 * @param   {Number} dist: distance along bearing in metres
 * @returns (LatLon} destination point
 */
function destVincenty(lat1, lon1, brng, dist) {
  var a = 6378137,
    b = 6356752.3142,
    f = 1 / 298.257223563; // WGS-84 ellipsiod
  var s = dist;
  var alpha1 = brng.toRad();
  var sinAlpha1 = Math.sin(alpha1);
  var cosAlpha1 = Math.cos(alpha1);

  var tanU1 = (1 - f) * Math.tan(lat1.toRad());
  var cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)),
    sinU1 = tanU1 * cosU1;
  var sigma1 = Math.atan2(tanU1, cosAlpha1);
  var sinAlpha = cosU1 * sinAlpha1;
  var cosSqAlpha = 1 - sinAlpha * sinAlpha;
  var uSq = cosSqAlpha * (a * a - b * b) / (b * b);
  var A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
  var B = uSq / 1024 *...