GoogleMaps添加色彩

时间:2016-03-28 14:20:34

标签: android google-maps

我想对围绕两个标记的GoogleMaps应用色调。到目前为止,我得到的唯一可行解决方案是在地图上实际绘制多边形

map.addPolygon(new PolygonOptions()
                .addAll(createRectangle(SphericalUtil.interpolate(markerData.getStartPoint(), markerData.getEndPoint(), CENTER_POINT_INTERPOLATE), 90, 45))
                .strokeColor(Color.TRANSPARENT)
                .fillColor(resources.getColor(R.color.tint_black_40)));

其中createRectangle()是来自https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/PolygonDemoActivity.java

的方法
private List<LatLng> createRectangle(LatLng center, double halfWidth, double halfHeight) {
    return Arrays.asList(new LatLng(center.latitude - halfHeight, center.longitude - halfWidth),
        new LatLng(center.latitude - halfHeight, center.longitude + halfWidth),
        new LatLng(center.latitude + halfHeight, center.longitude + halfWidth),
        new LatLng(center.latitude + halfHeight, center.longitude - halfWidth),
        new LatLng(center.latitude - halfHeight, center.longitude - halfWidth));
}

然而,创建的叠加层并不涵盖整个地图预览 map preview。是否有人可以帮助我改进现有方法或提出更好的解决方法?

2 个答案:

答案 0 :(得分:1)

您可以创建public void ZIPandGZIP(String fileName, OutputStream os, String header) throws FileNotFoundException { byte[] buffer = new byte[1024]; FileInputStream fis = null; FileOutputStream fos = null; ZipOutputStream zos = null; File tempZipFile = File.createTempFile(fileName, ".zip") try { fos = new FileOutputStream(tempZipFile); zos = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry(fileName); zos.putNextEntry(ze); fis = new FileInputStream(fileName); int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (fis != null) { fis.close(); } if (zos != null) { zos.close(); } if (fos != null) { fos.close(); } } addGzipFileToStream(tempZipFile, os, header); } private void addGzipFileToStream(File zipFile, OutputStream os, String header) throws FileNotFoundException { byte[] buffer = new byte[1024]; DataOutputStream dos = null; GZIPOutputStream gzos = null; FileInputStream inputStream = null; try { dos = new DataOutputStream(os); dos.writeBytes(header); gzos = new GZIPOutputStream(os); inputStream = new FileInputStream(zipFile); int len; while ((len = inputStream.read(buffer)) > 0) { gzos.write(buffer, 0, len); } gzos.finish(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (inputStream != null) { inputStream.close(); } if (gzos != null) { gzos.close(); } if (dos != null) { dos.close(); } zipFile.delete(); } } 并将其添加到地图中(在标记上方或下方,具体取决于zIndex)。

我们的想法是创建一个TileOverlay,它总是返回一个非常小的(在我的例子中为2x2像素)图像,该图像将在地图上绘制。为确保效果,TileProvider返回的Tile将始终相同。

以下是代码:

TileProvider

您需要将public class BackgroundTileProvider implements TileProvider { private Tile tile; @Override public Tile getTile(int x, int y, int zoom) { if (tile == null) { // Create a very small (for performance) bitmap with alpha Bitmap bmp = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888); // Draw the desired color to use as background Canvas canvas = new Canvas(bmp); canvas.drawColor(Color.argb(150, 0, 0, 0)); // Get the bytes and create the Tile ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); tile = new Tile(2, 2, stream.toByteArray()); } return tile; } } 添加到地图中,如下所示:

TileOverlay

答案 1 :(得分:1)

写下基本代码以创建具有色调效果的多边形。

private PolygonOptions mPolygonOptions;
private GoogleMap mGoogleMap;
private SupportMapFragment mMapFragment;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPolygonOptions = new PolygonOptions();
    }
@Override
    protected void onResume() {
        super.onResume();
        if(mGoogleMap == null) {
            mMapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.locationtrackmap));
            mGoogleMap = mMapFragment.getMap();
           mPolygonOptions.add(new LatLng(latitudeLongitude.One,latitudeLongitude.One));
           mPolygonOptions.add(new LatLng(latitudeLongitude.Two,latitudeLongitude.Two));
           mPolygonOptions.add(new LatLng(latitudeLongitude.Three,latitudeLongitude.Three));
           mPolygonOptions.add(new LatLng(latitudeLongitude.One,latitudeLongitude.One));
           mPolygonOptions.strokeColor(Color.RED);
           mPolygonOptions.strokeWidth(5);
           mPolygonOptions.fillColor(0x220000FF);
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLon_to_focus, 14.0f));
                mGoogleMap.addPolygon(mPolygonOptions);}
    }