AsyncTask根据逻辑没有正确执行

时间:2012-12-18 09:56:28

标签: android android-asynctask openstreetmap

基于具有纬度和经度值的给定地点集,我在尝试以不同缩放级别下载OSM的特定切片时遇到问题。

我要做的是确定顶部/左下角和顶部/右下角的MapTile编号,并循环编号以下载图块。目前我正在尝试下载构造函数中给定缩放级别上方的缩放级别1和1。

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;
}

在我的MainActivity类中,这就是我所谓的AsyncTask:

public class MainActivity extends Activity implements LocationListener, MapViewConstants {

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

当我为int x和int y(对于单个缩放图层)执行循环时,事情很好。然而,一旦我用int w放置第三个循环(为不同的缩放级别循环),事情开始变得混乱,它开始将每个单独的瓷砖下载到手机中。

我已经单独测试了代码逻辑(通过打印urlStr),它确实可以确定下载所需的特定MapTiles。但是当置于此AsyncTask类中时,相同的代码将无法工作,这使我相信这些代码可能在AsyncTask的实现方面存在问题。

希望有人可以指出我的错误。谢谢!

0 个答案:

没有答案