Adding an Authorization header in getTileUrl for Maps Tile Android - android

I would like to access some Custom Map Tiles when creating a TileOverlay for Google Maps API.
So this is my current code:
TileProvider tileProvider = new UrlTileProvider(256, 256) {
#Override
public URL getTileUrl(int x, int y, int z) {
String url = String.format("https://api.mycustommaps.com/v1/%d/%d/%d.jpg", z, x, y);
if (!checkTileExists(x, y, z)) {
return null;
}
try {
URL tileUrl = new URL(url);
tileUrl.openConnection().addRequestProperty("Authorization", LOGIN_TOKEN);
return tileUrl;
} catch (MalformedURLException e) {
e.printStackTrance();
} catch (IOException e) {
e.printStackTrance();
}
return null;
}
};
Since the connection returns 401 Anauthorized, I can't access the tiles. How could I pass Authorization header to let the url know I am authorized to access those tiles?

you have to implement the "TileProvider" interface, not URLTileProvider (because you have to retrieve the tile on your own, an URL is not enough.
https://developers.google.com/android/reference/com/google/android/gms/maps/model/TileProvider
as you can see, there is a note to keep attention:
Calls to methods in this interface might be made from multiple threads so implementations of this interface must be threadsafe.
and you have to implement a single method:
abstract Tile
getTile(int x, int y, int zoom)
It is now your work download the tile, I've done it for local files, so I'm just writing here some code that might need some more refinement and testing:
#Override
public Tile getTile(int x, int y, int zoom) {
String url = String.format("https://api.mycustommaps.com/v1/%d/%d/%d.jpg", z, x, y);
if (!checkTileExists(x, y, z)) {
return null;
}
try {
URL tileUrl = new URL(url);
//Download the PNG as byte[], I suggest using OkHTTP library or see next code!
final byte[] data = downloadData(tileUrl);
final int height = tileheight;
final int width = tilewidth;
if (data != null) {
if (BuildConfig.DEBUG)Log.d(TAG, "Cache hit for tile " + key);
return new Tile(width, height, data);
}
//In this case error, maybe return a placeholder tile or TileProvider.NO_TILE
} catch (MalformedURLException e) {
e.printStackTrance();
} catch (IOException e) {
e.printStackTrance();
}
}
to download:
byte[] downloadData(URL url){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
tileUrl.openConnection().addRequestProperty("Authorization", LOGIN_TOKEN);
is = url.openStream();
byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
baos.write(byteChunk, 0, n);
}
}
catch (IOException e) {
System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
e.printStackTrace ();
// Perform any other exception handling that's appropriate.
}
finally {
if (is != null) { is.close(); }
}
return baos.toByteArray():

Related

Draw a circle path in google map static api

I am developing an app using map static api in android
this is the business logic, get user location request from google static api with this location and draw a circle around this location
this is the code I am using
https://maps.googleapis.com/maps/api/staticmap?center=29.31166,47.481766&zoom=7&size=600x300&maptype=roadmap&key=My Key
the problem now how to draw a circle around it, I searched and I found it is done using path, but could not understand how to get that path
You need just draw path as in Developers Guide:
http://maps.googleapis.com/maps/api/staticmap?center=29.31166,47.48177&zoom=7&size=600x300&path=color:0x0000FFFF|weight:3|fillcolor:0x0000FF77|<FIRST_POINT_LAT>,<FIRST_POINT_LNG>|<SECOND_POINT_LAT>,<SECOND_POINT_LNG>|...|<LAST_POINT_LAT>,<LAST_POINT_LNG>&key=<YOUR_API_KEY>
where <FIRST_POINT_LAT>,<FIRST_POINT_LNG>|<SECOND_POINT_LAT>,<SECOND_POINT_LNG>|...|<LAST_POINT_LAT>,<LAST_POINT_LNG> is coordinates of your circle path. For it's calculation you can use method like that:
private List<LatLng> getCirclePoints(LatLng center, double radius) {
List<LatLng> circlePoints = new ArrayList<>();
// convert center coordinates to radians
double lat_rad = Math.toRadians(center.latitude);
double lon_rad = Math.toRadians(center.longitude);
double dist = radius / 6378137;
// calculate circle path point for each 5 degrees
for (int deg = 0; deg < 360; deg += 5) {
double rad = Math.toRadians(deg);
// calculate coordinates of next circle path point
double new_lat = Math.asin(Math.sin(lat_rad) * Math.cos(dist) + Math.cos(lat_rad) * Math.sin(dist) * Math.cos(rad));
double new_lon = lon_rad + Math.atan2(Math.sin(rad) * Math.sin(dist) * Math.cos(lat_rad), Math.cos(dist)
- Math.sin(lat_rad) * Math.sin(new_lat));
// convert new lat and lon to degrees
double new_lat_deg = Math.toDegrees(new_lat);
double new_lon_deg = Math.toDegrees(new_lon);
circlePoints.add(new LatLng(new_lat_deg, new_lon_deg));
}
return circlePoints;
}
And you can format Static Maps API URL with that points this way:
private String buildStaticApiUrlWithCircle(LatLng mapCenter, int zoom, int width, int height,
LatLng circleCenter, double circleRadius, int pathWeight, String pathColor, String fillColor) {
List<LatLng> circlePoints =getCirclePoints(circleCenter, circleRadius);
StringBuilder url = new StringBuilder();
url.append("http://maps.googleapis.com/maps/api/staticmap?");
url.append(String.format("center=%8.5f,%8.5f", mapCenter.latitude, mapCenter.longitude));
url.append(String.format("&zoom=%d", zoom));
url.append(String.format("&size=%dx%d", width, height));
// set circle path properties
url.append(String.format("&path="));
url.append(String.format("color:%s", pathColor));
url.append(String.format("|weight:%d", pathWeight));
url.append(String.format("|fillcolor:%s", fillColor));
// add circle path points
for (LatLng point : circlePoints) {
url.append(String.format("|%8.5f,%8.5f", point.latitude, point.longitude));
}
// add API key to URL
url.append(String.format("&key=%s", <YOUR_API_KEY>)));
return url.toString();
}
Circle path and fill colours should be set as String in "0xRRGGBBAA" format, where RR - value of red channel, GG - value of green channel, BB - value of blue channel and AA - value of alpha channel (e.g. "0x0000FFFF" - pure blue with no transparency, "0xFF000077" - pure red 50% transparent and so on).
When you use buildStaticApiUrlWithCircle() this way:
...
int mapZoom = 7;
int mapWidth = 600;
int mapHeight = 300;
LatLng mapCenter = new LatLng(29.31166, 47.481766);
LatLng circleCenter = new LatLng(29.376297, 47.976379);
double circleRadiusMerers = 35000;
String circlePathColor = "0x0000FFFF";
String circleFillColor = "0x0000FF99";
String mapUrl = buildStaticApiUrlWithCircle(mapCenter, mapZoom, mapWidth, mapHeight,
circleCenter, circleRadiusMerers, 3, circlePathColor, circleFillColor);
try {
Bitmap mapBitmap = new GetStaticMapAsyncTask().execute(mapUrl).get();
mMapImageView.setImageBitmap(mapBitmap);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
...
where GetStaticMapAsyncTask is:
private class GetStaticMapAsyncTask extends AsyncTask<String, Void, Bitmap> {
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
HttpURLConnection connection = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
InputStream stream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(stream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
}
}
you'll got something like that:
Also, you can use Google Maps Lite Mode instead of Static Map API (Lite Mode supports drawing circles). Or even, if you need draw circle exactly at the center of the map - direct drawing on bitmap canvas. For example you can modify doInBackground() of GetStaticMapAsyncTask this way:
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
HttpURLConnection connection = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
InputStream stream = connection.getInputStream();
Bitmap mapBitmap = BitmapFactory.decodeStream(stream);
Paint locaionMarkerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
locaionMarkerPaint.setColor(Color.BLUE);
bitmap = Bitmap.createBitmap(mapBitmap.getWidth(), mapBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(mapBitmap,0,0, null);
canvas.drawCircle(mapBitmap.getWidth()/ 2, mapBitmap.getHeight() / 2, 20, locaionMarkerPaint);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return bitmap;
}

Google Maps offline Mode on Android Application

Is there any posibility to show Google Maps if you are offline in your own App?
What about if I download an Area FROM Google Maps application for offline mode, could i visualize the map on the app that i develop if i don't have internet connection?
if not, What options do i have to make this possible? I just want to visualize the map when my app is offline...
The following its the code that this post provided TileProvider using local tiles
#Override
public Tile getTile(int x, int y, int zoom) {
byte[] image = readTileImage(x, y, zoom);
return image == null ? null : new Tile(TILE_WIDTH, TILE_HEIGHT,image);
}
private byte[] readTileImage(int x,int y, int zoom){
InputStream is= null;
ByteArrayOutputStream buffer= null;
try{
is= mAssets.open(getTileFileName(x,y,zoom));
buffer= new ByteArrayOutputStream();
int nRead;
byte[] data= new byte[BUFFER_SIZE];
while ((nRead= is.read(data,0,BUFFER_SIZE)) !=-1){
buffer.write(data,0,nRead);
}
buffer.flush();
return buffer.toByteArray();
}
catch(IOException ex){
Log.e("LINE 60 CustomMap", ex.getMessage());
return null;
}catch(OutOfMemoryError e){
Log.e("LINE 64 CustomMap", e.getMessage());
return null;
}finally{
if(is!=null){
try{
is.close();
} catch (IOException e) {}
}
if(buffer !=null){
try{
buffer.close();
}catch (Exception e){}
}
}
}
private String getTileFileName(int x, int y, int zoom){
return "map/"+ zoom +'/' +x+ '/'+y+".png";
}
I was looking for information, and My questions is, how can i download the tiles?
I was facing the same challenge, and none of the examples I found included a complete implementation of downloading the tiles, writing them to file and reading them from file.
This is my code, which reads the tile from file when it's available locally and downloads/saves the tile when not. This uses the OpenStreetMap.org tile server, but you could use any server you like by changing the URL.
private class OfflineTileProvider implements TileProvider {
private static final String TILES_DIR = "your_tiles_directory/";
private static final int TILE_WIDTH = 256;
private static final int TILE_HEIGHT = 256;
private static final int BUFFER_SIZE_FILE = 16384;
private static final int BUFFER_SIZE_NETWORK = 8192;
private ConnectivityManager connectivityManager;
#Override
public Tile getTile(int x, int y, int z) {
Log.d(TAG, "OfflineTileProvider.getTile(" + x + ", " + y + ", " + z + ")");
try {
byte[] data;
File file = new File(TILES_DIR + z, x + "_" + y + ".png");
if (file.exists()) {
data = readTile(new FileInputStream(file), BUFFER_SIZE_FILE);
} else {
if (connectivityManager == null) {
connectivityManager = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
}
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
Log.w(TAG, "No network");
return NO_TILE;
}
Log.d(TAG, "Downloading tile");
data = readTile(new URL("https://a.tile.openstreetmap.org/" +
z + "/" + x + "/" + y + ".png").openStream(),
BUFFER_SIZE_NETWORK);
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
out.write(data);
}
}
return new Tile(TILE_WIDTH, TILE_HEIGHT, data);
} catch (Exception ex) {
Log.e(TAG, "Error loading tile", ex);
return NO_TILE;
}
}
private byte[] readTile(InputStream in, int bufferSize) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
int i;
byte[] data = new byte[bufferSize];
while ((i = in.read(data, 0, bufferSize)) != -1) {
buffer.write(data, 0, i);
}
buffer.flush();
return buffer.toByteArray();
} finally {
in.close();
buffer.close();
}
}
}
Replace "your_tiles_directory" with the path to the directory where you want to store your tiles.
To use the TileProvider:
map.setMapType(GoogleMap.MAP_TYPE_NONE);
offlineTileOverlay = map.addTileOverlay(new TileOverlayOptions()
.tileProvider(new OfflineTileProvider()));
Edit: You may want to set the max zoom level, the default is 21 but OpenStreetMap for example seems to have a maximum of 19.
map.setMaxZoomPreference(19);
You can download tile image from a tile server and cache on your app. Check some server on this link. Or you can build a tile as this demo, then download tile image from it.
Good luck

android image not appearing when getting bytes from socket and using bitmap creation

I have a program in which I'm trying to download the content of an image file from a server. I'm using java socket to download it. After downloading, I use BitmapFactory.decodeByteArray() to create a bitmap.
At the server side, the file is a .jpg file and it's only about 180 KBytes, so I don't need to try scaling it. I can see through logs that the exact number of bytes in the file is received by my image download code. I store all the bytes in a byte[] array and then convert it into a bitmap.
The imageView is initially hidden and then supposed to be made visible after populating the image. But using BitmapFactory.decodeByteArray() is returning null always. I did see some other posts about null bitmap, but nothing seems to have an answer for this problem.
I don't want to use any external library just for this, so please do not give me suggestions to try out some other libraries. Can someone spot any problem with the code? The server side program is also mine and I know that part is correct because using that, browsers are able to download the same image file. I have copy-pasted it below.
public class ImageDownloader {
private Socket sockToSrvr;
private PrintWriter strmToSrvr;
private BufferedInputStream strmFromSrvr;
private String srvrAddr;
private int port;
private String remoteFile;
private Context ctxt;
private Bitmap imgBmap;
private View parkSpotImgVwHldr;
private View mngAndFndVwHldr;
private View parkSpotImgVw;
public ImageDownloader(Context c) {
srvrAddr = KloudSrvr.srvrIp();
port = KloudSrvr.port();
sockToSrvr = null;
strmFromSrvr = null;
strmToSrvr = null;
remoteFile = null;
ctxt = c;
imgBmap = null;
parkSpotImgVwHldr = null;
mngAndFndVwHldr = null;
parkSpotImgVw = null;
}
public void downloadFile(String remf, View parkSpotImgVwHldrVal,
View mngAndFndVwHldrVal, View parkSpotImgVwVal) {
remoteFile = remf;
parkSpotImgVwHldr = parkSpotImgVwHldrVal;
mngAndFndVwHldr = mngAndFndVwHldrVal;
parkSpotImgVw = parkSpotImgVwVal;
Thread dwnThrd = new Thread() {
#Override
public void run() {
imgBmap = null;
openServerConnection(); sendReq(); doDownload(); closeServerConnection();
((Activity)ctxt).runOnUiThread(new Runnable() {
public void run() {
((Activity)ctxt).runOnUiThread(new Runnable() {
public void run() {
mngAndFndVwHldr.setVisibility(View.GONE);
parkSpotImgVwHldr.setVisibility(View.VISIBLE);
Toast.makeText(ctxt, "completed", Toast.LENGTH_LONG).show();
}
});
}
});
}
};
dwnThrd.start();
}
private void sendReq() {
if(strmToSrvr == null) return;
String req = "GET /downloadFile " + remoteFile + " HTTP/1.1\r\n\r\n";
Log.d("IMG-DWNL-LOG: ", "writing req msg to socket " + req);
strmToSrvr.write(req); strmToSrvr.flush();
}
private void doDownload() {
boolean gotContLen = false;
int contLen = 0;
while(true) {
String inLine = getLine(strmFromSrvr); if(inLine == null) break;
if((gotContLen == true) &&
(inLine.replace("\r", "").replace("\n", "").isEmpty() == true)) break;
if(inLine.trim().startsWith("Content-Length:") == true) {
// an empty line after this signifies start of content
String s = inLine.replace("Content-Length:", "").trim();
try {contLen = Integer.valueOf(s); gotContLen = true; continue;}
catch(NumberFormatException nfe) {contLen = 0;}
}
}
if((gotContLen == false) || (contLen <= 0)) return;
byte[] imgByts = new byte[contLen];
int totRdByts = 0, rdByts, chnk = 1024, avlByts;
while(true) {
try {
avlByts = strmFromSrvr.available(); if(avlByts < 0) break;
if(avlByts == 0) {try {Thread.sleep(1000);} catch(InterruptedException ie) {} continue;}
rdByts = (avlByts < chnk) ? avlByts : chnk;
rdByts = strmFromSrvr.read(imgByts, totRdByts, rdByts); if(rdByts < 0) break;
if(rdByts == 0) {try {Thread.sleep(1000);} catch(InterruptedException ie) {} continue;}
totRdByts += rdByts;
if(totRdByts >= contLen) break;
} catch(IOException ioe) {return;}
}
if(totRdByts < contLen) {
Log.d("IMG-DWNL-LOG: ", "error - bytes read " + totRdByts
+ " less than content length " + contLen);
return;
}
if(totRdByts <= 0) return;
Log.d("IMG-DWNL-LOG: ", "read all image bytes successfully, setting image into view");
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(imgByts, 0, contLen, options);
if(bitmap == null) {Log.d("IMG-DWNL-LOG: ", "got a null bitmap");}
((ImageView)parkSpotImgVw).setImageBitmap(bitmap);
}
private void closeServerConnection() {
if(sockToSrvr == null) return;
if(strmFromSrvr != null) {
try {strmFromSrvr.close();}
catch(IOException e) {Log.d("IMG-DWNL-LOG: ", "Inp strm close exception");}
}
if(strmToSrvr != null) strmToSrvr.close();
try {sockToSrvr.close();}
catch(IOException e) {Log.d("IMG-DWNL-LOG: ", "Conn close exception");}
strmFromSrvr = null; strmToSrvr = null; sockToSrvr = null;
}
private void openServerConnection() {
try {sockToSrvr = new Socket(InetAddress.getByName(srvrAddr), port);}
catch(UnknownHostException e) {
Log.d("IMG-DWNL-LOG: ", "Unknown host exception"); sockToSrvr = null; return;
} catch(IOException e) {
Log.d("IMG-DWNL-LOG: ", "Server connect exception"); sockToSrvr = null; return;
}
Log.d("IMG-DWNL-LOG: ", "Connected to server");
try {
strmFromSrvr = new BufferedInputStream(sockToSrvr.getInputStream());
strmToSrvr = new PrintWriter(new BufferedWriter(new OutputStreamWriter
(sockToSrvr.getOutputStream())), true);
} catch(IOException e) {
closeServerConnection();
Log.d("IMG-DWNL-LOG: ", "Failed to open reader / writer. Closed the connection."); return;
}
}
private String getLine(BufferedInputStream dis) {
String outLine = "";
while(true) {
try {
int c = dis.read(); if((c == -1) && (outLine.length() <= 0)) return(null);
outLine += Character.toString((char)c);
if(c == '\n') return(outLine);
} catch(IOException e) {if(outLine.length() <= 0) return(null); return(outLine);}
}
}
}
I was making a mistake, assuming that a .jpg file's bytes can be directly decode with the android bitmap decoder. Apparently this is not the case. So, I wrote the received bytes into a temporary file in the phone storage and then called BitmapFactory.decodeFile() which is able to return a good bitmap and ends up showing the image.
So, have a working solution now.
Still - if anyone has a better suggestion how to decode directly from the received bytes (which are from a .jpg file), I would be very interested to try it out since that would be more efficient. Thanks.

Android Printing Framework - Landscape PDF file gets rotated sideways and gets clipped in preview

I have the following code for to print a PDF file using the Android Printing Framework:
Here is my PrintDocumentAdapter class:
#TargetApi(Build.VERSION_CODES.KITKAT)
public class PrintPDFAdapter extends PrintDocumentAdapter {
private File pdfFile;
private String fileName;
public PrintPDFAdapter(File pdfFile, String fileName) {
this.pdfFile = pdfFile;
this.fileName = fileName;
}
#Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder(fileName).setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
#Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(pdfFile);
output = new FileOutputStream(destination.getFileDescriptor());
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
} catch (FileNotFoundException ee){
//Catch exception
} catch (Exception e) {
//Catch exception
} finally {
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Here is my method that calls the PrintManager print command:
#TargetApi(Build.VERSION_CODES.KITKAT)
private void doPDFPrint(File pdfFile, String filename) {
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) + " Report";
PrintPDFAdapter pda = new PrintPDFAdapter(pdfFile, filename);
PrintAttributes attrib = new PrintAttributes.Builder().
setMediaSize(PrintAttributes.MediaSize.NA_LETTER.asLandscape()).
setMinMargins(PrintAttributes.Margins.NO_MARGINS).
build();
printManager.print(jobName, pda, attrib);
}
The PDF that I am trying to print is in landscape orientation. For some reason, when sending it to print, the PDF file gets rotated sideways and gets clipped. I would like to know, is there anything I can do to get around this?
UPDATE: I just did a test print, and it actually prints properly. However, the preview shows it rotated sideways.
In my case it was because I'm using my own transform, and when supplying a transform, the native renderer does not apply the page rotation specified in the PDF page's attributes. There is no way to get this attribute via the Android API right now, so I used iText:
private PdfReader _iTextReader;
...
_iTextReader = new PdfReader(file.getAbsolutePath());
...
// iText uses 1-indexed pages
PdfDictionary iTextPage = _iTextReader.getPageN(pageNumber + 1);
PdfNumber rotate = iTextPage.getAsNumber(PdfName.ROTATE);
if (rotate != null && rotate.intValue() % 360 != 0) {
// getPageSize returns media box
float mediaBoxWidth = _iTextReader.getPageSize(pageNumber + 1).getWidth();
float mediaBoxHeight = _iTextReader.getPageSize(pageNumber + 1).getHeight();
float iTextCenterX = mediaBoxWidth / 2;
float iTextCenterY = mediaBoxHeight / 2;
Util.Log("Applying " + rotate.intValue() + " degree rotation per PDF page attributes.");
pdfMatrix.postRotate(rotate.intValue(), iTextCenterX, iTextCenterY);
/*
* Transform it back to the top-left corner.
* For the life of me, I do not know why these translations are right. But I've
* test both portrait->landscape and landscape->portrait on all rotation angles.
*/
if (rotate.intValue() == 90) {
pdfMatrix.postTranslate(iTextCenterX - iTextCenterY, iTextCenterX - iTextCenterY);
} else if (rotate.intValue() == 270) {
pdfMatrix.postTranslate(3 * iTextCenterY - 3 * iTextCenterX, iTextCenterX - iTextCenterY);
}
}
Here's the link to the source code of the native PDF renderer: https://github.com/android/platform_frameworks_base/blob/master/core/jni/android/graphics/pdf/PdfRenderer.cpp

Google Maps API Android - Tile provider's tiles resolution

I'm using custom TileProviders in my Android app to display offline maps and OpenStreetMap maps. It works, but there is a problem with the tiles resolution, which is quite bad. The files have a size of 256x256, and setting the width/height of my TileProvider to 128 doesn't change anything.
Here is some piece of code :
public class GenericUrlTileProvider extends UrlTileProvider {
// ---------------------------------------------------------------------------------------
// Private attributes :
private String _baseUrl;
// ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Constructor :
public GenericUrlTileProvider(int width, int height, String url) {
super(width, height);
this._baseUrl = url;
}
#Override
public URL getTileUrl(int x, int y, int zoom) {
try {
return new URL(_baseUrl.replace("{z}", "" + zoom).replace("{x}", "" + x).replace("{y}", "" + y));
}
catch (MalformedURLException e) { e.printStackTrace(); }
return null;
}
// ---------------------------------------------------------------------------------------
}
Does anyone know how to fix this to support high resolution devices ?
Thanks
On #grub request, here is what I did for getting the 4 tiles of the next zoom level :
public Tile getTileFromNextZoomLevel(int x, int y, int zoom) {
final String topLeftTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2)).replace("{y}", "" + (y * 2));
final String topRightTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2 + 1)).replace("{y}", "" + (y * 2));
final String bottomLeftTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2)).replace("{y}", "" + (y * 2 + 1));
final String bottomRightTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2 + 1)).replace("{y}", "" + (y * 2 + 1));
final Bitmap[] tiles = new Bitmap[4];
Thread t1 = new Thread() {
#Override
public void run() { tiles[0] = Utils.getBitmapFromURL(topLeftTileUrl); }
};
t1.start();
Thread t2 = new Thread() {
#Override
public void run() { tiles[1] = Utils.getBitmapFromURL(topRightTileUrl); }
};
t2.start();
Thread t3 = new Thread() {
#Override
public void run() { tiles[2] = Utils.getBitmapFromURL(bottomLeftTileUrl); }
};
t3.start();
Thread t4 = new Thread() {
#Override
public void run() { tiles[3] = Utils.getBitmapFromURL(bottomRightTileUrl); }
};
t4.start();
try {
t1.join();
t2.join();
t3.join();
t4.join();
}
catch (InterruptedException e) { e.printStackTrace(); }
byte[] tile = Utils.mergeBitmaps(tiles, Bitmap.CompressFormat.JPEG); // PNG is a lot slower, use it only if you really need to
return tile == null ? TileProvider.NO_TILE : new Tile( (int) _source.getTileSize().getWidth(), (int) _source.getTileSize().getHeight(), tile);
}
And the Utils methods :
public static byte[] mergeBitmaps(Bitmap[] parts, Bitmap.CompressFormat format) {
// Check if all the bitmap are null (if so return null) :
boolean allNulls = true;
for (int i = 0; i < parts.length; i++) {
if(parts[i] != null) {
allNulls = false;
break;
}
}
if(allNulls) return null;
Bitmap tileBitmap = Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(tileBitmap);
Paint paint = new Paint();
for (int i = 0; i < parts.length; i++) {
if(parts[i] == null) {
parts[i] = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
}
canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
tileBitmap.compress(format, 100, stream);
byte[] bytes = stream.toByteArray();
return bytes;
}
public static Bitmap getBitmapFromURL(String urlString) {
try {
// Ensure the file exists :
if(Utils.getResponseCode(urlString) != 200) return null;
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());
return bitmap;
}
catch (IOException e) { return null; }
}
You may have to adapt it for your needs. Please note that my app is still under development, and this code may need some tests / improvements.

Categories

Resources