AsyncTask not executing properly according to logic

假如想象 提交于 2019-12-12 05:52:55

问题


I'm having problems trying to download specific tiles for OSM in different zoom levels, based on a given set of places with their latitude and longitude values.

What I am trying to do is to determine the MapTile number for the top/bottom left and top/bottom right corners, and loop the numbers to download the tiles. For now I am trying to download zoom levels 1 above and 1 below the given zoom level in the constructor.

public class MapDownload extends AsyncTask<String, Void, String>{

int zoom;
private ArrayList<GeoPoint> places;
private Coordinates topRight = new Coordinates(); // a java class I did for myself
private Coordinates bottomRight = new Coordinates();
private Coordinates topLeft = new Coordinates();
private Coordinates bottomLeft = new Coordinates();

public MapDownload(ArrayList<GeoPoint> placeList, int zoom){ 
    this.places = placeList;
    this.zoom = zoom;
}

@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub

        for (int w = zoom -1 ; w <= zoom +1; w++){

            double maxLat = 0.0;
            double maxLon = 0.0;
            double minLat = 0.0;
            double minLon = 0.0;

            for(GeoPoint point: places) {
                double lon = (double) ( point.getLongitudeE6() / 1E6 * 1.0);
                double lat = (double) (point.getLatitudeE6() / 1E6 * 1.0);
                    if(lat > maxLat) {
                        maxLat = lat;
                    }
                    if(lat < minLat || minLat == 0.0) {
                        minLat = lat;
                    }
                    if(lon> maxLon) {
                        maxLon = lon;
                    }
                    if(lon < minLon || lon == 0.0) {
                        minLon = lon;
                    }
            }
            topRight = topRight.gpsToMaptile(maxLon, maxLat, w); //top right
            bottomRight = bottomRight.gpsToMaptile(maxLon, minLat, w); //bottom right
            topLeft = topLeft.gpsToMaptile(minLon, maxLat, w); //top left
            bottomLeft = bottomLeft.gpsToMaptile(minLon, minLat, w); //bottom left

            for (int x = topLeft.getYTile(); x < bottomLeft.getYTile(); x++){
                for(int y = topLeft.getXTile(); y < bottomRight.getXTile(); y++){
                    try {
                        String urlStr = "http://a.tile.openstreetmap.org/"+ w +"/"+y+"/"+x+".png";
                        URL url = new URL(urlStr);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                        File newFileDir = new File(Environment.getExternalStorageDirectory().toString() 
                            + "/downloadMap/test/"+w+"/"+y);
                        newFileDir.mkdirs();
                        File newFile = new File(newFileDir, x+".png");
                        OutputStream output = new FileOutputStream(newFile);
                        int read;
                        while ((read = in.read()) != -1) {
                            output.write(read);
                            output.flush();
                        }
                        urlConnection.disconnect();
                } catch (Exception e) {
                    Log.e("URL::: ERROR", e.getMessage());
                    e.printStackTrace();
                }
            }

        }
    }
    return null;
}

In my MainActivity class, this is what I did to call this AsyncTask:

public class MainActivity extends Activity implements LocationListener, MapViewConstants {

    public void onCreate(Bundle savedInstanceState) {
        MapDownload mapDownload = new MapDownload(placeList, 12);
        mapDownload.execute("");  
    }
}

Things were fine when I did the loops for int x and int y (for a single zoom layer). However once I placed the 3rd loop with int w (to loop for different zoom levels), things started going haywire and it started downloading every single tile into the phone.

I have tested for the code logic separately (by printing the urlStr) and it does indeed work to determine the specific MapTiles needed for download. However the same codes wouldn't work when placed in this AsyncTask class, which led me to believe that such codes might have a problem with the implementation of AsyncTask.

Hope there is someone out there whom could point out my mistake. Thank you!

来源:https://stackoverflow.com/questions/13930364/asynctask-not-executing-properly-according-to-logic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!