Geo-Google-PolylineEncoder

 view release on metacpan or  search on metacpan

t/js_reference/polyline.js  view on Meta::CPAN

  var txtValue = document.getElementById('txtNumber').value;
  if (parseInt(txtValue).toString() == txtValue) {
    document.getElementById('cdeValue').innerHTML
      = encodeNumber(parseInt(txtValue));
  }else{
    document.getElementById('cdeValue').innerHTML = '(None)';
  }
}

// Try to encode a signed number. Used by the documentation.
function trySignEncode() {
  var txtValue = document.getElementById('txtSignNumber').value;
  if (parseInt(txtValue).toString() == txtValue) {
    document.getElementById('cdeSignValue').innerHTML
      = encodeSignedNumber(parseInt(txtValue));
  }else{
    document.getElementById('cdeSignValue').innerHTML = '(None)';
  }
}

// Create the encoded polyline and level strings. If moveMap is true
// move the map to the location of the first point in the polyline.
function createEncodings(moveMap) {
  var i = 0;

  var plat = 0;
  var plng = 0;

  var encoded_points = "";
  var encoded_levels = "";

  for(i = 0; i < points.length; ++i) {
    var point = points[i];
    var lat = point.Latitude;
    var lng = point.Longitude;
    var level = point.Level;

    var late5 = Math.round(lat * 1e5);
    var lnge5 = Math.round(lng * 1e5);

    dlat = late5 - plat;
    dlng = lnge5 - plng;

    plat = late5;
    plng = lnge5;

    encoded_points += encodeSignedNumber(dlat) + encodeSignedNumber(dlng);
    encoded_levels += encodeNumber(level);
  }

  // move if moveMap is true.
  if (moveMap) {
    document.map.setCenter(
        new GLatLng(points[0].Latitude, points[0].Longitude),
        document.map.getZoom());
  }

  document.getElementById('encodedLevels').value = encoded_levels;
  document.getElementById('encodedPolyline').value = encoded_points;

  if (document.overlay) {
    document.map.removeOverlay(document.overlay);
  }

  if (points.length > 1) {
    document.overlay = GPolyline.fromEncoded({color: "#0000FF",
                                              weight: 10,
                                              points: encoded_points,
                                              zoomFactor: 32,
                                              levels: encoded_levels,
                                              numLevels: 4
                                             });

    document.map.addOverlay(document.overlay);
  }
}

function centerMap() {
  var address = document.getElementById('txtAddress').value;

  if (address.length > 0) {
    var geocoder = new GClientGeocoder();

    geocoder.getLatLng(address,
      function(point) {
        if (!point) {
          alert('Address "' + address + '" not found');
        } else {
          document.map.setCenter(point, 13);
        }
      });
  }
}

// Decode an encoded polyline into a list of lat/lng tuples.
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;
}

// Decode an encoded levels string into a list of levels.
function decodeLevels(encoded) {
  var levels = [];

t/js_reference/polyline.js  view on Meta::CPAN


  var enc_points = decodeLine(encoded_points);
  var enc_levels = decodeLevels(encoded_levels);

  if (enc_points.length==0 || enc_levels.length==0) {
    return;
  }

  if (enc_points.length != enc_levels.length) {
    alert('Point count and level count do not match');
    return;
  }


  deleteAllMarkers();
  document.getElementById('pointList').options.length = 0;
  document.getElementById('txtPointsList').value = "";
  points = [];

  pointsStr = "";
  for (var i = 0; i < enc_points.length; ++i) {
    createPoint(enc_points[i][0], enc_points[i][1], enc_levels[i]);
    pointsStr += enc_points[i][0] + "," + enc_points[i][1] + "," + enc_levels[i] + "\n";
  }

  document.getElementById('txtPointsList').value = pointsStr;

  createEncodings(true);
}

function createMarker(point, color) {
  var f = new GIcon();
  f.image = "http://labs.google.com/ridefinder/images/mm_20_" + color
            + ".png";
  f.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  f.iconSize = new GSize(12,20);
  f.shadowSize = new GSize(22,20);
  f.iconAnchor = new GPoint(6,20);
  f.infoWindowAnchor = new GPoint(6,1);
  f.infoShadowAnchor = new GPoint(13,13);

  newMarker = new GMarker(point,
    {icon: f,
     draggable: true});

  return newMarker;
}

// Create the Google Map to be used.
function createMap() {
  if (!GBrowserIsCompatible()) {
    alert('Your browser is not compatible with the Google Maps API');
    return;
  }

  document.map = new GMap2(document.getElementById("map_canvas"));
  document.map.setCenter(new GLatLng(37.4419, -122.1419), 13);
  document.map.addControl(new GSmallMapControl());
  document.map.addControl(new GMapTypeControl());

  GEvent.addListener(document.map, "click", function(overlay, point) {
    document.getElementById('txtLatitude').value = point.y;
    document.getElementById('txtLongitude').value = point.x;

    if (marker == null) {
      marker = createMarker(point, "green");
      marker.enableDragging();

      GEvent.addListener(marker, "drag", function() {
        document.getElementById('txtLatitude').value = marker.getPoint().y;
        document.getElementById('txtLongitude').value = marker.getPoint().x;
      });

      document.map.addOverlay(marker);
    } else {
      marker.setPoint(point);
    }
  });
}

// Move the map to the selected point in the point list.
function jumpToPoint() {
  var pointList = document.getElementById('pointList');
  if (pointList.selectedIndex >= 0) {
    var point = points[pointList.selectedIndex];
    document.map.setCenter(new GLatLng(point.Latitude, point.Longitude),
                           document.map.getZoom());
  }
}



( run in 2.006 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )