マピオンAPI


Android Maps APIドキュメント

独自地図画像を指定する

public class MainActivity extends MapActivity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Map map = new MapionTownMap("APIキー", R.drawable.nashi);
		
		MapView mapView = new MapView(this, map);
		mapView.setClickable(true);
		mapView.setBuiltInZoomControls(true);
		setContentView(mapView);
	}
}
public class MapionTownMap extends Map {

	private static final int TILE_WIDTH = 330;
	private static final int TILE_HEIGHT = 223;

	private static HashMap cols = new HashMap();

	private static HashMap ratios = new HashMap();

    private static HashMap tileNameMap = new HashMap();

    private String key;
    
    private int noimage;

    public MapionTownMap(String key, int noimage) {
            this.key = key;
            this.noimage = noimage;
            init();
    }

    private void init() {
            cols.put(1, 1);
            cols.put(2, 2);
            cols.put(3, 4);

            ratios.put(1, 4d);
            ratios.put(2, 2d);
            ratios.put(3, 1d);
    }

    @Override
    protected int getMaxZoomLevel() {
            return ratios.size();
    }

    @Override
    protected void setup() {
            setCopyright(HttpClient
                            .getBitmap("http://labs.mapion.co.jp/blog/honjo/mapioncopyright.png"));
            setCenter(new GeoPoint(0, 0));
            setZoom(1);
    }

    @Override
    protected int getTileWidth() {
            return TILE_WIDTH;
    }

    @Override
    protected int getTileHeight() {
            return TILE_HEIGHT;
    }

    @Override
    protected Point geoToPixel(GeoPoint geo) {
            int x = (int) (geo.getLongitudeE6() / getRatio());
            int y = (int) (geo.getLatitudeE6() / getRatio());
            return new Point(x, y);
    }

    @Override
    protected GeoPoint pixelToGeo(Point pixel) {
            int lat = (int) (pixel.y * getRatio());
            int lon = (int) (pixel.x * getRatio());
            return new GeoPoint(lat, lon);
    }

    @Override
    protected GeoPoint getOrigin() {
            double originRatio = ratios.get(1);
            int lat = (int) ((TILE_HEIGHT / 2) * originRatio);
            int lon = (int) (-(TILE_WIDTH / 2) * originRatio);
            return new GeoPoint(lat, lon);
    }

    @Override
    protected String getURL(Tile tile) {
            if (isOut(tile)) {
                    return noMap();
            }
            int x = tile.getX();
            int y = tile.getY();
            int tileNo = y * cols.get(getZoom()) + x;
            String name = "level" + getZoom() + "_" + tileNo;
            int id = getResourceInt(name);
            if (id == -1) {
                    return noMap();
            } else {
                    String ret = String.valueOf(id);
                    return ret;
            }
    }

    @Override
    protected String getKey() {
            return key;
    }

    private double getRatio() {
            return ratios.get(getZoom());
    }

    private int getResourceInt(String name) {
            if (tileNameMap.containsKey(name)) {
                    return tileNameMap.get(name);
            }
            try {
                    Field field = R.drawable.class.getDeclaredField(name);
                    int tileId = field.getInt(R.drawable.class);
                    tileNameMap.put(name, tileId);
                    return tileId;
            } catch (Exception e) {
            }
            return -1;
    }

    private String noMap() {
            return String.valueOf(noimage);
    }

    private boolean isOut(Tile tile) {
            int x = tile.getX();
            int y = tile.getY();
            if (x < 0 || y < 0) {
                    return true;
            }
            if (x >= cols.get(getZoom())) {
                    return true;
            }
            return false;
    }
}