緯度と経度の値を持つ特定の場所のセットに基づいて、さまざまなズームレベルで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("");
}
}
intxとinty(単一のズームレイヤーの場合)のループを実行したときは、問題はありませんでした。ただし、int wを使用して3番目のループを配置すると(さまざまなズームレベルでループするため)、問題が発生し始め、すべてのタイルが電話にダウンロードされ始めました。
私は(urlStrを印刷することによって)コードロジックを個別にテストしましたが、ダウンロードに必要な特定のMapTilesを決定するために実際に機能します。ただし、このAsyncTaskクラスに配置すると同じコードが機能しないため、このようなコードではAsyncTaskの実装に問題がある可能性があると私は考えました。
私の間違いを指摘してくれる人がそこにいることを願っています。ありがとうございました!