mercoledì 14 marzo 2012

Esperimenti di mappe

Ho fatto qualche primo esperimento con Osmdroid, una libreria Android che permette di gestire mappe offline in particolare quelle di OpenStreetMap

 
La procedura prevede l'importazione di due librerie nella Build Path e cambiare il Manifest ma per il resto e' piuttosto banale. I dati vanno inseriti nella SD card nella cartella osmdroid

 

OSMMapActivity.java
 
 
package com.test.osm;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;

import android.os.Bundle;

public class OSMMapActivity extends Activity {
    // The MapView variable:
    private MapView m_mapView;

    // Default map zoom level:
    private int MAP_DEFAULT_ZOOM = 14;

    // Default map Latitude:
    private double MAP_DEFAULT_LATITUDE =  43.8304;

    // Default map Longitude:
    private double MAP_DEFAULT_LONGITUDE = 11.3079;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Specify the XML layout to use:
        setContentView(R.layout.main);

        //copy from assets to sd card
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
         files = assetManager.list("");
        } catch (IOException e){
         
        }
        for (String filename : files){
         InputStream in = null;
         OutputStream out = null;
         File osmdir = new File(Environment.getExternalStorageDirectory(),"osmdroid");
         if (!osmdir.exists()) osmdir.mkdirs();
         
         try {
          in = assetManager.open(filename);
          out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/osmdroid/"+filename);
          byte[] buffer = new byte[1024];
          int read;
          while ((read=in.read(buffer))!=-1){
           out.write(buffer,0,read);
          }        
         } catch (IOException e){}
        }
        
        // Find the MapView controller in that layout:
        m_mapView = (MapView) findViewById(R.id.mapview);
        

        // Setup the mapView controller:
        m_mapView.setBuiltInZoomControls(true);
        m_mapView.setMultiTouchControls(true);
        m_mapView.setClickable(true);
        m_mapView.setUseDataConnection(false);
        m_mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
        m_mapView.getController().setCenter(
            new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));
        m_mapView.setTileSource(TileSourceFactory.MAPNIK);
    } // end onCreate()
} // end class YourMap


syntax highlighted by Code2HTML, v. 0.9.1