Code example for Matrix

Methods: mapPoints, postRotate, setPolyToPoly

0
    if (geoToMetric == null) {
      initializeGeoToMetricMatrix(); 
    } 
    // Map the given location to image space and check if it fits within the image area 
    float[] location = { longitude, latitude };
    geoToMetric.mapPoints(location);
    return (0 < location[0] && location[0] < metricSize[0] &&
            0 < location[1] && location[1] < metricSize[1]);
  } 
 
  /** 
   * Finds the shortest distance from the given point to the map area. If the 
   * given point is within the map boundaries, returns 0. The returned distance 
   * is in meters. 
   * 
   * @param longitude of the location to check 
   * @param latitude of the location to check 
   * @return distance in meters from the point to the edge of the map, or 0 if 
   *         the point is within the map boundaries 
   */ 
  public float getDistanceFrom(float longitude, float latitude) {
    // Return 0 if user is within map area 
    if (this.contains(longitude, latitude)) {
      return 0f; 
    } 
    // TODO: This initial implementation doesn't consider 180 longitude 
    if (geoToMetric == null) {
      initializeGeoToMetricMatrix(); 
    } 
    final int X = 0;
    final int Y = 1;
    float[] location = { longitude, latitude };
    geoToMetric.mapPoints(location);
    // Find minimum distance to the map rectangle 
    if (location[X] < 0) {
      // User is past left edge of map 
      if (location[Y] < 0) {
        // above and left: closest map point is (0, 0) 
        return getGeometricDistance(location[X], location[Y]);
      } else if (location[Y] > metricSize[Y]) {
        // below and left: closest map point is (0, metricSize[Y]) 
        return getGeometricDistance(location[X], location[Y] - metricSize[Y]);
      } else { 
        // straight left: straight distance to left edge 
        return -location[X];
      } 
    } 
    if (metricSize[X] < location[X]) {
      // User is past right edge of map 
      if (location[Y] < 0) {
        // above and right: closest map point is (metricSize[X], 0) 
        return getGeometricDistance(location[X] - metricSize[X], location[Y]);
      } else if (location[Y] > metricSize[Y]) {
        // below and right: closest map point is (metricSize[X], metricSize[Y]) 
        return getGeometricDistance(location[X] - metricSize[X], location[Y] - metricSize[Y]);
      } else { 
        // straight right: straight distance to right edge 
        return location[X] - metricSize[X];
      } 
    } 
    // location[X] is within map area, get straight distance to closer edge 
    if (location[Y] < 0) {
      // User is directly above map area 
      return -location[Y];
    } else { 
      // User is directly below map area 
      return location[Y] - metricSize[Y];
    } 
  } 
 
  // Finds shortest distance from point (x, y) to line connecting points (x0, y0) and (x1, y1) 
  public float getGeometricDistance(float xDiff, float yDiff) {
    return (float) Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  } 
 
  private void initializeGeoToMetricMatrix() { 
    metricSize = getMapImageSize();
    float metricWidth = metricSize[0];
    float metricHeight = metricSize[1];
    float[] imageCorners = {
        0, 0,   metricWidth, 0,   metricWidth, metricHeight,   0, metricHeight
    }; 
    float[] geoPoints;
    if (hasCornerTiePoints()) { 
      geoPoints = new float[] {
          northWestCornerLonLat[0], northWestCornerLonLat[1],
          northEastCornerLonLat[0], northEastCornerLonLat[1],
          southEastCornerLonLat[0], southEastCornerLonLat[1],
          southWestCornerLonLat[0], southWestCornerLonLat[1]
      }; 
    } else { 
      geoPoints = new float[] {
          west, north,   east, north,   east, south,   west, south
      }; 
    } 
    Matrix matrix = new Matrix();
    matrix.setPolyToPoly(geoPoints, 0, imageCorners, 0, 4);
    if (!hasCornerTiePoints() && getRotateAngle() != 0) { 
      matrix.postRotate(getRotateAngle(), metricWidth / 2, metricHeight / 2);
    } 
    geoToMetric = matrix;
  } 
 
  // Returns the map image size (width, height) in meters