在Maps Tile Android的getTileUrl中添加授权标头 [英] Adding an Authorization header in getTileUrl for Maps Tile Android

查看:160
本文介绍了在Maps Tile Android的getTileUrl中添加授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在为Google Maps API创建TileOverlay时访问一些自定义地图块。



所以这是我当前的代码:

  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); $!
$ b if(!checkTileExists(x,y,z)){
return null;
}

尝试{
URL tileUrl = new URL(url);
tileUrl.openConnection()。addRequestProperty(Authorization,LOGIN_TOKEN);
返回tileUrl;
} catch(MalformedURLException e){
e.printStackTrance();
} catch(IOException e){
e.printStackTrance();
}
返回null;
}
};

由于连接返回401未授权,我无法访问切片。我怎么能通过授权标题让网址知道我有权访问这些瓷砖?

你必须实现 TileProvider界面,而不是URLTileProvider(因为您必须自行检索拼贴,但URL不够。
https://developers.google.com/android/reference/com/google/android/gms/maps/model/TileProvider
就像你看到的那样,有一个注意要注意:


调用此接口中的方法可能是由多个线程构成的,


您必须实现一个方法:


抽象Tile
getTile(int x,int y,int zoom)



<现在你的工作下载了瓷砖,我不知道e为本地文件,所以我只是在这里写一些代码,可能需要进一步改进和测试:

  @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); $!
$ b if(!checkTileExists(x,y,z)){
return null;
}

尝试{
URL tileUrl = new URL(url);
//将PNG下载为byte [],我建议使用OkHTTP库或参阅下面的代码!
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);
返回新的Tile(宽度,高度,数据);
}
//在这种情况下,错误可能会返回一个占位符tile或TileProvider.NO_TILE

} catch(MalformedURLException e){
e.printStackTrance();
} catch(IOException e){
e.printStackTrance();


$ / code $ / pre

下载:

  byte [] downloadData(URL url){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream是= null;
尝试{
tileUrl.openConnection()。addRequestProperty(Authorization,LOGIN_TOKEN);
is = url.openStream();
byte [] byteChunk =新字节[4096]; //或者您想一次读入的任何大小。
int n; ((n = is.read(byteChunk))> 0){
baos.write(byteChunk,0,n);



$ b catch(IOException e){
System.err.printf(从%s读取字节时失败:%s,url.toExternalForm(),e .getMessage());
e.printStackTrace();
//执行适当的其他异常处理。
}
finally {
if(is!= null){is.close(); }
}
return baos.toByteArray():


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():

这篇关于在Maps Tile Android的getTileUrl中添加授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆