如何在 android 中保存 google 地图?以及如何使用 tileprovider
How to save google map in android ?and How to use tileprovider
我正在使用互联网创建 Android Google 地图,但我不知道如何在 Android 应用程序中保存 Google 地图。
示例:Android 地图应用。
请帮帮我
你可以试试static maps, you can save your map as image. Also user can save offline maps on his own: downloading offline maps.
代码示例:
public Bitmap getBitmap() {
Bitmap bmp = null;
try {
List<LatLng> places = getPlaces();
String mapUrl = "https://maps.googleapis.com/maps/api/staticmap?size=600x600&maptype=roadmap";
for (LatLng place : places) {
mapUrl += "&markers=" + place.latitude + "," + place.longitude;
}
InputStream in = new URL(mapUrl).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception ex) {
// log error
}
return bmp;
}
您可以抓取来自不同来源的磁贴。这是 TileProvider 的实现,您可以将其与 googleMap 一起使用,从而获取 GoogleMapTiles
public class GoogleMapOfflineTileProvider implements TileProvider
{
private static final String UPPER_ZOOM_TILE_URL;
static
{
UPPER_ZOOM_TILE_URL = "http://mt0.google.com/vt/lyrs=m&hl=ru&x=%d&y=%d&z=%d&scale=1&s=Galileo";
}
private static final String TAG = GoogleMapOfflineTileProvider.class.getName();
private SQLiteMapDatabase sqLiteMapDatabase;
public GoogleMapOfflineTileProvider(File file)
{
this(file.getAbsolutePath());
}
public GoogleMapOfflineTileProvider(String pathToFile)
{
sqLiteMapDatabase = new SQLiteMapDatabase();
sqLiteMapDatabase.setFile(new File(pathToFile));
}
@Override
public Tile getTile(int x, int y, int z)
{
Tile tile = NO_TILE;
if ( sqLiteMapDatabase.existsTile(x, y, z) )
{
tile = new Tile(256, 256, sqLiteMapDatabase.getTile(x, y, z));
}
else if ( z < 11 )
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
getBitmapFromURL(x, y, z).compress(Bitmap.CompressFormat.JPEG, 100, stream);
tile = new Tile(256, 256, stream.toByteArray());
}
return tile;
}
public static Bitmap getBitmapFromURL(int x, int y, int z)
{
Log.d(TAG, String.format(UPPER_ZOOM_TILE_URL, x, y, z));
try
{
URL url = new URL(String.format(UPPER_ZOOM_TILE_URL, x, y, z));
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
return BitmapFactory.decodeStream(connection.getInputStream());
}
catch (IOException e)
{
Log.d(TAG, "exception when retrieving bitmap from internet" + e.toString());
return null;
}
}
}
因此您可以通过 x/y 和缩放将切片缓存到您的数据库中。或者您可以保存特定区域。在重用它们之后。要为 google 个地图设置它,请使用:
tileProvider = new GoogleMapOfflineTileProvider(file);
offlineOverlay = googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider).zIndex(3000));
重要提示:根据google许可协议,您只能暂时在移动设备上使用这种缓存。
我正在使用互联网创建 Android Google 地图,但我不知道如何在 Android 应用程序中保存 Google 地图。
示例:Android 地图应用。
请帮帮我
你可以试试static maps, you can save your map as image. Also user can save offline maps on his own: downloading offline maps.
代码示例:
public Bitmap getBitmap() {
Bitmap bmp = null;
try {
List<LatLng> places = getPlaces();
String mapUrl = "https://maps.googleapis.com/maps/api/staticmap?size=600x600&maptype=roadmap";
for (LatLng place : places) {
mapUrl += "&markers=" + place.latitude + "," + place.longitude;
}
InputStream in = new URL(mapUrl).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception ex) {
// log error
}
return bmp;
}
您可以抓取来自不同来源的磁贴。这是 TileProvider 的实现,您可以将其与 googleMap 一起使用,从而获取 GoogleMapTiles
public class GoogleMapOfflineTileProvider implements TileProvider
{
private static final String UPPER_ZOOM_TILE_URL;
static
{
UPPER_ZOOM_TILE_URL = "http://mt0.google.com/vt/lyrs=m&hl=ru&x=%d&y=%d&z=%d&scale=1&s=Galileo";
}
private static final String TAG = GoogleMapOfflineTileProvider.class.getName();
private SQLiteMapDatabase sqLiteMapDatabase;
public GoogleMapOfflineTileProvider(File file)
{
this(file.getAbsolutePath());
}
public GoogleMapOfflineTileProvider(String pathToFile)
{
sqLiteMapDatabase = new SQLiteMapDatabase();
sqLiteMapDatabase.setFile(new File(pathToFile));
}
@Override
public Tile getTile(int x, int y, int z)
{
Tile tile = NO_TILE;
if ( sqLiteMapDatabase.existsTile(x, y, z) )
{
tile = new Tile(256, 256, sqLiteMapDatabase.getTile(x, y, z));
}
else if ( z < 11 )
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
getBitmapFromURL(x, y, z).compress(Bitmap.CompressFormat.JPEG, 100, stream);
tile = new Tile(256, 256, stream.toByteArray());
}
return tile;
}
public static Bitmap getBitmapFromURL(int x, int y, int z)
{
Log.d(TAG, String.format(UPPER_ZOOM_TILE_URL, x, y, z));
try
{
URL url = new URL(String.format(UPPER_ZOOM_TILE_URL, x, y, z));
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
return BitmapFactory.decodeStream(connection.getInputStream());
}
catch (IOException e)
{
Log.d(TAG, "exception when retrieving bitmap from internet" + e.toString());
return null;
}
}
}
因此您可以通过 x/y 和缩放将切片缓存到您的数据库中。或者您可以保存特定区域。在重用它们之后。要为 google 个地图设置它,请使用:
tileProvider = new GoogleMapOfflineTileProvider(file);
offlineOverlay = googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider).zIndex(3000));
重要提示:根据google许可协议,您只能暂时在移动设备上使用这种缓存。