Commit 6007cd68 authored by Vytor Calixto's avatar Vytor Calixto 👾

Commit que deu meio boa

IBAGENS APARECEM!!! NADA ACONTECE
parent deb1ca34
......@@ -6,6 +6,7 @@ import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
......@@ -40,8 +41,9 @@ public class MainActivity extends Activity {
private void initViews(){
tilesProvider = new TilesProvider(getApplicationContext());
Display display = getWindowManager().getDefaultDisplay();
mapView = new MapView(this, display.getWidth(), display.getHeight(), tilesProvider);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mapView = new MapView(this, displayMetrics.widthPixels, displayMetrics.heightPixels, tilesProvider);
if(savedGpsLocation != null) {
mapView.setGpsLocation(savedGpsLocation);
......
......@@ -40,6 +40,8 @@ public class MapView extends View {
this.viewHeight = viewHeight;
this.viewWidth = viewWidth;
Log.d(MainActivity.TAG, "width: " + viewWidth + " height: " + viewHeight);
tilesManager = new TilesManager(256, viewWidth, viewHeight);
initPaints();
......@@ -61,46 +63,52 @@ public class MapView extends View {
fontPaint.setShadowLayer(1, 1, 1, Color.BLACK);
fontPaint.setTextSize(20);
circlePaint.setARGB(70, 170, 170, 80);
circlePaint.setARGB(80, 112, 33, 255);
circlePaint.setAntiAlias(true);
}
private void fetchTiles() {
tilesManager.setLocation(seekLocation);
Rect regiaoVisivel = tilesManager.getAreaVisivel();
tilesProvider.fetchTiles(regiaoVisivel, tilesManager.getZoom());
Log.d(MainActivity.TAG, "regiaoVisivel");
Log.d(MainActivity.TAG, regiaoVisivel.toString());
Log.d(MainActivity.TAG, "Quantas tiles?");
Log.d(MainActivity.TAG, String.valueOf(tilesProvider.tiles.size()));
Rect rect = new Rect(0, 0, 2, 2);
tilesProvider.fetchTiles(rect, tilesManager.getZoom());
Log.d(MainActivity.TAG, "zoom: " + tilesManager.getZoom());
Log.d(MainActivity.TAG, "regiaoVisivel: " + regiaoVisivel.toString());
Log.d(MainActivity.TAG, "Quantas tiles? " + tilesProvider.getTiles().values().size());
}
public void drawTiles(Canvas canvas, Point offset){
Collection<Tile> tilesList = tilesProvider.getTiles().values();
Log.d(MainActivity.TAG, "tilesList.size(): " + tilesList.size());
Log.d(MainActivity.TAG, "regiaoVisivel: " + tilesManager.getAreaVisivel().toString());
for(Tile tile : tilesList) {
int tileSize = tilesManager.getTamTile();
long tileX = tile.getX() * tileSize;
long tileY = tile.getY() * tileSize;
Log.d(MainActivity.TAG, "offset: " + offset + " tileX, tileY: " + tileX + " ," + tileY);
long finalX = tileX - offset.x;
long finalY = tileY - offset.y;
long finalX = tileX ;//- offset.x;
long finalY = tileY ;//- offset.y;
canvas.drawBitmap(tile.getImg(), finalX, finalY, bitmapPaint);
Log.d(MainActivity.TAG, "finalX, finalY: " + finalX + ", " + finalY);
canvas.drawBitmap(tile.getImg(), finalY, finalX, bitmapPaint);
}
}
public void drawMarker(Canvas canvas, Point offset) {
if(gpsLocation != null) {
Point markerPos = tilesManager.longLatToXY(gpsLocation.getLongitude(), gpsLocation.getLatitude());
int markerX = markerPos.x - offset.x;
int markerY = markerPos.y - offset.y;
int markerX = markerPos.x ;//- offset.x;
int markerY = markerPos.y ;//- offset.y;
Log.d(MainActivity.TAG, "Marker(x,y): (" + markerX + ", " + markerY + ")");
float ground = (float) tilesManager.calcGroundResolution(gpsLocation.getLatitude());
float rad = gpsLocation.getAccuracy() / ground;
Log.d(MainActivity.TAG, "raio: " + rad);
canvas.drawCircle(markerX, markerY, rad, circlePaint);
canvas.drawCircle(markerX, markerY, 5, circlePaint);
int pen = 1;
canvas.drawText("lon:" + gpsLocation.getLongitude(), 0, 20 * pen++, fontPaint);
......@@ -118,7 +126,7 @@ public class MapView extends View {
int mapWidth = tilesManager.tamanhoMapa() * 256;
Point centerPixel = new Point((int) (centerRatio.getY() * mapWidth), (int) (centerRatio.getY() * mapWidth));
Point centerPixel = new Point((int) (centerRatio.getX() * mapWidth), (int) (centerRatio.getY() * mapWidth));
Point offset = new Point((int) (centerPixel.x - viewWidth / 2f), (int) (centerPixel.y - viewHeight / 2f));
......
......@@ -2,44 +2,61 @@ package ufpr.inf.pet.geoloc;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import java.math.BigDecimal;
/**
* Created by vmocelin on 27/05/15.
*/
public class TilesManager {
private static double raioTerra = 6378137;
private static double minLatitude = -25.444282186891535;
private static double maxLatitude = -25.457806121916125;
private static double minLongitude = -49.22666788101196;
private static double maxLongitude = -49.24293279647827;
// private static BigDecimal minLatitude = new BigDecimal("-25.444282186891535");
// private static BigDecimal maxLatitude = new BigDecimal("-25.457806121916125");
// private static BigDecimal minLongitude = new BigDecimal("-49.24293279647827");
// private static BigDecimal maxLongitude = new BigDecimal("-49.22666788101196");
private static BigDecimal minLatitude = new BigDecimal("-90");
private static BigDecimal maxLatitude = new BigDecimal("90");
private static BigDecimal minLongitude = new BigDecimal("-180");
private static BigDecimal maxLongitude = new BigDecimal("180");
private int maxZoom = 5;
private int tamTile = 256;
private int viewWidth;
private int viewHeight;
private int tilesX;
private int tilesY;
private int zoom = 0;
private int zoom = 1;
private PointD location = new PointD(0,0);
private Rect areaVisivel;
public TilesManager(int tamTile, int viewWidth, int viewHeight) {
//Define a escala em 15 casas decimais com o arrendodamente ROUND_HALF_EVEN (melhor para não acumular erros segundo o Javadoc)
minLatitude.setScale(15, BigDecimal.ROUND_HALF_EVEN);
maxLatitude.setScale(15, BigDecimal.ROUND_HALF_EVEN);
minLongitude.setScale(15, BigDecimal.ROUND_HALF_EVEN);
maxLongitude.setScale(15, BigDecimal.ROUND_HALF_EVEN);
this.tamTile = tamTile;
this.viewHeight = viewHeight;
this.viewWidth = viewWidth;
tilesX = (int) ((float) viewWidth / tamTile);
tilesY = (int) ((float) viewHeight / tamTile);
Log.d(MainActivity.TAG, "tilesX: " + tilesX + " tilesY: " + tilesY);
atualizaAreaVisivel(location.getX(), location.getY(), zoom);
atualizaAreaVisivel(location.getX(), location.getY(), this.zoom);
}
private void atualizaAreaVisivel(double longitude, double latitude, int zoom) {
location.setX(longitude);
location.setY(latitude);
Log.d(MainActivity.TAG, "Location: " + location.toString());
this.zoom = zoom;
Point tileId = getTileXY(location.getX(), location.getY());
Log.d(MainActivity.TAG, "tileId: " + tileId.toString());
int meioTileX = (int) ((float) (tilesX + 1)/2f);
int meioTileY = (int) ((float) (tilesY + 1)/2f);
......@@ -54,9 +71,16 @@ public class TilesManager {
// Retorna um ponto, cujas coordenadas variam de 0 a 1, dando assim uma proporção do mapa
public static PointD calcRatio(double longitude, double latitude) {
Log.d(MainActivity.TAG, "longitude: " + longitude + " latitude: " + latitude);
// Para o eixo X, basta uma simples regra de 3
// (Longitude - minLongitude) / (maxLongitude - minLongitude)
double ratioX = (longitude - minLongitude) / (maxLongitude - minLongitude);
BigDecimal lon = new BigDecimal(longitude);
lon.setScale(15, BigDecimal.ROUND_HALF_EVEN);
BigDecimal numerador = lon.subtract(minLongitude);
Log.d(MainActivity.TAG, "numerador: " + numerador);
BigDecimal denominador = maxLongitude.subtract(minLongitude);
Log.d(MainActivity.TAG, "denominador: " + denominador);
BigDecimal ratioX = numerador.divide(denominador, 15, BigDecimal.ROUND_HALF_EVEN);
// Para o eixo Y é necessária certa matemágica trigonométrica
// (0.5 - log((1+senoLatitude)/(1-senoLatitude)))/4*pi
......@@ -64,7 +88,8 @@ public class TilesManager {
double senoLatitude = Math.sin(latitude * Math.PI / 180.0);
double ratioY = (0.5 - Math.log((1 + senoLatitude)/(1 - senoLatitude))) / (4 * Math.PI);
return new PointD(ratioX, ratioY);
Log.d(MainActivity.TAG, "ratioX: " + ratioX.doubleValue() + " ratioY: " + ratioY);
return new PointD(ratioX.doubleValue(), ratioY);
}
// Dado uma longitude e latitude, retorna o indice (ponto) que determina um tile
......@@ -80,8 +105,8 @@ public class TilesManager {
}
public Point longLatToXY(double latitude, double longitude){
latitude = clip(latitude, minLatitude, maxLatitude);
longitude = clip(longitude, minLongitude, maxLongitude);
latitude = clip(latitude, minLatitude.doubleValue(), maxLatitude.doubleValue());
longitude = clip(longitude, minLongitude.doubleValue(), maxLongitude.doubleValue());
double x = (longitude + 180) / 360;
double sinLatitude = Math.sin(latitude * Math.PI / 180);
......@@ -107,52 +132,40 @@ public class TilesManager {
public double calcGroundResolution(double latitude)
{
latitude = clip(latitude, minLatitude, maxLatitude);
latitude = clip(latitude, minLatitude.doubleValue(), maxLatitude.doubleValue());
return Math.cos(latitude * Math.PI / 180.0) * 2.0 * Math.PI * raioTerra / (double) (tamTile * tamanhoMapa());
}
public int zoomIn()
{
setZoom(++zoom);
if(zoom + 1 <= 5) {
setZoom(++zoom);
}
return zoom;
}
public int zoomOut()
{
setZoom(--zoom);
if(zoom - 1 > 0) {
setZoom(--zoom);
}
return zoom;
}
public double getMinLatitude() {
return minLatitude;
}
public void setMinLatitude(double minLatitude) {
this.minLatitude = minLatitude;
return minLatitude.doubleValue();
}
public double getMaxLatitude() {
return maxLatitude;
}
public void setMaxLatitude(double maxLatitude) {
this.maxLatitude = maxLatitude;
return maxLatitude.doubleValue();
}
public double getMinLongitude() {
return minLongitude;
}
public void setMinLongitude(double minLongitude) {
this.minLongitude = minLongitude;
return minLongitude.doubleValue();
}
public double getMaxLongitude() {
return maxLongitude;
}
public void setMaxLongitude(double maxLongitude) {
this.maxLongitude = maxLongitude;
return maxLongitude.doubleValue();
}
public int getMaxZoom() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment