@Override
 public void onDestroy() {
   super.onDestroy();
   System.gc();
 }
  public void run() {
    int x, y;
    Bitmap locTile;
    Canvas canvas;
    float useLon, useLat, useLonMax, useLatMax;

    System.gc();
    if ((useOverlay.tileX == 0) && (useOverlay.tileY == 0)) return;
    mapThreadRunning = true;
    try {
      if (locMap == null)
        locMap = Bitmap.createBitmap(tileWidth * 256, tileHeight * 256, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError oome) {
      return;
    }
    locMap.eraseColor(Color.BLACK);
    canvas = new Canvas(locMap);

    useLon =
        (GeoUtils.tilex2long(useOverlay.tileX, useOverlay.m_zoom)
                + GeoUtils.tilex2long(useOverlay.tileX + 1, useOverlay.m_zoom))
            / 2.0f;
    useLat =
        (GeoUtils.tiley2lat(useOverlay.tileY, useOverlay.m_zoom)
                + GeoUtils.tiley2lat(useOverlay.tileY + 1, useOverlay.m_zoom))
            / 2.0f;

    while (mScaleFactor >= 2.0) {
      mScaleFactor /= 2.0f;
      useOverlay.m_zoom++;
      if (useOverlay.m_zoom > 17) {
        useOverlay.m_zoom = 17;
        imgOffsetX -= (scrWidth - (scrWidth * 2.0)) / 2.0;
        imgOffsetY -= (scrHeight - (scrHeight * 2.0)) / 2.0;
      }
    }
    while (mScaleFactor <= 0.5) {
      mScaleFactor *= 2.0f;
      useOverlay.m_zoom--;
      if (useOverlay.m_zoom < 3) {
        useOverlay.m_zoom = 3;
        imgOffsetX -= (scrWidth - (scrWidth * 2.0)) / 2.0;
        imgOffsetY -= (scrHeight - (scrHeight * 2.0)) / 2.0;
      }
    }
    mScaleFactor = 1.0f;

    /*      if ((wasScaleOp) && (lonCenter!=0.0f) && (latCenter!=0.0f))
    {
       tileX=GeoUtils.long2tilex(lonCenter,m_zoom);
       tileY=GeoUtils.lat2tiley(latCenter,m_zoom);

       tileX-=(scrWidth / 256);
       tileY-=(scrWidth / 256);
    }
    else*/
    {
      useOverlay.tileX = GeoUtils.long2tilex(useLon, useOverlay.m_zoom);
      useOverlay.tileY = GeoUtils.lat2tiley(useLat, useOverlay.m_zoom);
    }
    useLon = GeoUtils.tilex2long(useOverlay.tileX - 1, useOverlay.m_zoom);
    useLat = GeoUtils.tiley2lat(useOverlay.tileY - 1, useOverlay.m_zoom);
    useLonMax = GeoUtils.tilex2long(useOverlay.tileX + tileWidth + 1, useOverlay.m_zoom);
    useLatMax = GeoUtils.tiley2lat(useOverlay.tileY + tileHeight + 1, useOverlay.m_zoom);

    /*      if (imgOffsetX>scrWidth)
    {
       imgOffsetX=scrWidth/2;
    }
    if (imgOffsetX<=-scrWidth)
    {
       imgOffsetX=-scrWidth/2;
    }
    if (imgOffsetY>scrHeight)
    {
       imgOffsetY=scrHeight/2;
    }
    if (imgOffsetY<=-scrHeight)
    {
       imgOffsetY=-scrHeight/2;
    }*/

    while (imgOffsetX > 0) {
      imgOffsetX -= 256.0f;
      useOverlay.tileX--;
    }
    while (imgOffsetX <= -256.0) {
      imgOffsetX += 256.0f;
      useOverlay.tileX++;
    }
    while (imgOffsetY > 0) {
      imgOffsetY -= 256.0f;
      useOverlay.tileY--;
    }
    while (imgOffsetY <= -256.0) {
      imgOffsetY += 256.0f;
      useOverlay.tileY++;
    }

    // calculate lat/lon of screen center point
    /*      {
       int useTileX=tileX,useTileY=tileY;

       x=(int)((scrWidth/2.0f)+imgOffsetX);
       y=(int)((scrHeight/2.0f)+imgOffsetY);

       useTileX+=(x / 256);
       x=x % 256;
       useTileY+=(y / 256);
       y=y % 256;

       float tileLon1=GeoUtils.tilex2long(useTileX,m_zoom);
       float tileLat1=GeoUtils.tiley2lat(useTileY,m_zoom);
       float tileLon2=GeoUtils.tilex2long(useTileX+1,m_zoom);
       float tileLat2=GeoUtils.tiley2lat(useTileY+1,m_zoom);

       lonCenter=(((tileLon2-tileLon1)/256.0f)*x)+tileLon1;
       latCenter=(((tileLat2-tileLat1)/256.0f)*y)+tileLat1;
    }*/

    matrix.setTranslate(imgOffsetX, imgOffsetY);
    breakMapThread = false;
    mapThreadRunning = false;
    for (x = 0; x < tileWidth; x++)
      for (y = 0; y < tileHeight; y++) {
        if (breakMapThread) return;
        locTile =
            geoUtils.loadMapTile(
                getContext(),
                useOverlay.tileX + x,
                useOverlay.tileY + y,
                useOverlay.m_zoom,
                allowNetAccess);
        if (locTile != null) canvas.drawBitmap(locTile, x * 256, y * 256, null);
        postInvalidate();
      }
    useOverlay.doDraw(canvas, useLon, useLonMax, useLat, useLatMax, locMap);
    postInvalidate();
    System.gc();
  }