Search in sources :

Example 1 with Tile

use of com.google.android.gms.maps.model.Tile in project collect by opendatakit.

the class GoogleMapsMapBoxOfflineTileProvider method getTile.

// ------------------------------------------------------------------------
// TileProvider Interface
// ------------------------------------------------------------------------
@Override
public Tile getTile(int x, int y, int z) {
    Tile tile = NO_TILE;
    if (this.isZoomLevelAvailable(z) && this.isDatabaseAvailable()) {
        String[] projection = { "tile_data" };
        int row = ((int) (Math.pow(2, z) - y) - 1);
        String predicate = "tile_row = ? AND tile_column = ? AND zoom_level = ?";
        String[] values = { String.valueOf(row), String.valueOf(x), String.valueOf(z) };
        Cursor c = this.database.query("tiles", projection, predicate, values, null, null, null);
        if (c != null) {
            c.moveToFirst();
            if (!c.isAfterLast()) {
                tile = new Tile(256, 256, c.getBlob(0));
            }
            c.close();
        }
    }
    return tile;
}
Also used : Tile(com.google.android.gms.maps.model.Tile) Cursor(android.database.Cursor)

Example 2 with Tile

use of com.google.android.gms.maps.model.Tile in project wigle-wifi-wardriving by wiglenet.

the class MappingFragment method setupMapView.

private void setupMapView(final View view, final LatLng oldCenter, final int oldZoom) {
    // view
    final RelativeLayout rlView = (RelativeLayout) view.findViewById(R.id.map_rl);
    if (mapView != null) {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mapView.setLayoutParams(params);
    }
    // conditionally replace the tile source
    final SharedPreferences prefs = getActivity().getSharedPreferences(ListFragment.SHARED_PREFS, 0);
    rlView.addView(mapView);
    // guard against not having google play services
    mapView.getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(final GoogleMap googleMap) {
            if (ActivityCompat.checkSelfPermission(MappingFragment.this.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(MappingFragment.this.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                googleMap.setMyLocationEnabled(true);
            }
            googleMap.setBuildingsEnabled(true);
            final boolean showTraffic = prefs.getBoolean(ListFragment.PREF_MAP_TRAFFIC, true);
            googleMap.setTrafficEnabled(showTraffic);
            final int mapType = prefs.getInt(ListFragment.PREF_MAP_TYPE, GoogleMap.MAP_TYPE_NORMAL);
            googleMap.setMapType(mapType);
            mapRender = new MapRender(getActivity(), googleMap, false);
            googleMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {

                @Override
                public boolean onMyLocationButtonClick() {
                    if (!state.locked) {
                        state.locked = true;
                        if (menu != null) {
                            MenuItem item = menu.findItem(MENU_TOGGLE_LOCK);
                            String name = state.locked ? getString(R.string.menu_turn_off_lockon) : getString(R.string.menu_turn_on_lockon);
                            item.setTitle(name);
                            MainActivity.info("on-my-location received - activating lock");
                        }
                    }
                    return false;
                }
            });
            googleMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {

                @Override
                public void onCameraMoveStarted(int reason) {
                    if (reason == REASON_GESTURE) {
                        if (state.locked) {
                            state.locked = false;
                            if (menu != null) {
                                MenuItem item = menu.findItem(MENU_TOGGLE_LOCK);
                                String name = state.locked ? getString(R.string.menu_turn_off_lockon) : getString(R.string.menu_turn_on_lockon);
                                item.setTitle(name);
                            }
                        }
                    } else if (reason == REASON_API_ANIMATION) {
                    // DEBUG: MainActivity.info("Camera moved due to user tap");
                    // TODO: should we combine this case with REASON_GESTURE?
                    } else if (reason == REASON_DEVELOPER_ANIMATION) {
                    // MainActivity.info("Camera moved due to app directive");
                    }
                }
            });
            // controller
            final LatLng centerPoint = getCenter(getActivity(), oldCenter, previousLocation);
            float zoom = DEFAULT_ZOOM;
            if (oldZoom >= 0) {
                zoom = oldZoom;
            } else {
                zoom = prefs.getFloat(ListFragment.PREF_PREV_ZOOM, zoom);
            }
            final CameraPosition cameraPosition = new CameraPosition.Builder().target(centerPoint).zoom(zoom).build();
            googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            if (!ListFragment.PREF_MAP_NO_TILE.equals(prefs.getString(ListFragment.PREF_SHOW_DISCOVERED, ListFragment.PREF_MAP_NO_TILE))) {
                final int providerTileRes = MainActivity.isHighDefinition() ? 512 : 256;
                // TODO: DRY up token composition vs AbstractApiRequest?
                String ifAuthToken = null;
                try {
                    final String authname = prefs.getString(ListFragment.PREF_AUTHNAME, null);
                    final String token = TokenAccess.getApiToken(prefs);
                    if ((null != authname) && (null != token)) {
                        final String encoded = Base64.encodeToString((authname + ":" + token).getBytes("UTF-8"), Base64.NO_WRAP);
                        ifAuthToken = "Basic " + encoded;
                    }
                } catch (UnsupportedEncodingException ueex) {
                    MainActivity.error("map tiles: unable to encode credentials for mine/others", ueex);
                } catch (UnsupportedOperationException uoex) {
                    MainActivity.error("map tiles: unable to access credentials for mine/others", uoex);
                }
                final String authToken = ifAuthToken;
                final String userAgent = AbstractApiRequest.getUserAgentString();
                TileProvider tileProvider = new TileProvider() {

                    @Override
                    public Tile getTile(int x, int y, int zoom) {
                        if (!checkTileExists(x, y, zoom)) {
                            return null;
                        }
                        final Long since = prefs.getLong(ListFragment.PREF_SHOW_DISCOVERED_SINCE, 2001);
                        int thisYear = Calendar.getInstance().get(Calendar.YEAR);
                        String tileContents = prefs.getString(ListFragment.PREF_SHOW_DISCOVERED, ListFragment.PREF_MAP_NO_TILE);
                        String sinceString = String.format("%d0000-00000", since);
                        String toString = String.format("%d0000-00000", thisYear + 1);
                        String s = String.format(MAP_TILE_URL_FORMAT, zoom, x, y, sinceString, toString);
                        if (MainActivity.isHighDefinition()) {
                            s += HIGH_RES_TILE_TRAILER;
                        }
                        // ALIBI: defaults to "ALL"
                        if (ListFragment.PREF_MAP_ONLYMINE_TILE.equals(tileContents)) {
                            s += ONLY_MINE_TILE_TRAILER;
                        } else if (ListFragment.PREF_MAP_NOTMINE_TILE.equals(tileContents)) {
                            s += NOT_MINE_TILE_TRAILER;
                        }
                        try {
                            final byte[] data = downloadData(new URL(s), userAgent, authToken);
                            if (data != null) {
                                return new Tile(providerTileRes, providerTileRes, data);
                            }
                        } catch (MalformedURLException e) {
                            throw new AssertionError(e);
                        }
                        return null;
                    }

                    /*
                         * depends on supported levels on the server
                         */
                    private boolean checkTileExists(int x, int y, int zoom) {
                        int minZoom = 0;
                        int maxZoom = 24;
                        if ((zoom < minZoom || zoom > maxZoom)) {
                            return false;
                        }
                        return true;
                    }

                    private byte[] downloadData(final URL url, final String userAgent, final String authToken) {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        InputStream is = null;
                        try {
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            if (null != authToken) {
                                conn.setRequestProperty("Authorization", authToken);
                            }
                            conn.setRequestProperty("User-Agent", userAgent);
                            is = conn.getInputStream();
                            byte[] byteChunk = new byte[4096];
                            int n;
                            while ((n = is.read(byteChunk)) > 0) {
                                baos.write(byteChunk, 0, n);
                            }
                        } catch (IOException e) {
                            MainActivity.error("Failed while reading bytes from " + url.toExternalForm() + ": " + e.getMessage());
                            e.printStackTrace();
                        } finally {
                            if (is != null) {
                                try {
                                    is.close();
                                } catch (IOException ioex) {
                                    MainActivity.error("Failed while closing InputStream " + url.toExternalForm() + ": " + ioex.getMessage());
                                    ioex.printStackTrace();
                                }
                            }
                        }
                        return baos.toByteArray();
                    }
                };
                tileOverlay = googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider).transparency(0.35f));
            }
        }
    });
    MainActivity.info("done setupMapView.");
}
Also used : MalformedURLException(java.net.MalformedURLException) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) TileProvider(com.google.android.gms.maps.model.TileProvider) URL(java.net.URL) CameraPosition(com.google.android.gms.maps.model.CameraPosition) HttpURLConnection(java.net.HttpURLConnection) GoogleMap(com.google.android.gms.maps.GoogleMap) LatLng(com.google.android.gms.maps.model.LatLng) TileOverlayOptions(com.google.android.gms.maps.model.TileOverlayOptions) SharedPreferences(android.content.SharedPreferences) ViewGroup(android.view.ViewGroup) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Tile(com.google.android.gms.maps.model.Tile) MenuItem(android.view.MenuItem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RelativeLayout(android.widget.RelativeLayout)

Example 3 with Tile

use of com.google.android.gms.maps.model.Tile in project wigle-wifi-wardriving by wiglenet.

the class HeatmapTileProvider method convertBitmap.

/**
 * helper function - convert a bitmap into a tile
 *
 * @param bitmap bitmap to convert into a tile
 * @return the tile
 */
private static Tile convertBitmap(Bitmap bitmap) {
    // Convert it into byte array (required for tile creation)
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
    return new Tile(TILE_DIM, TILE_DIM, bitmapdata);
}
Also used : Tile(com.google.android.gms.maps.model.Tile) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with Tile

use of com.google.android.gms.maps.model.Tile in project android-maps-utils by googlemaps.

the class HeatmapTileProvider method convertBitmap.

/**
 * helper function - convert a bitmap into a tile
 *
 * @param bitmap bitmap to convert into a tile
 * @return the tile
 */
private static Tile convertBitmap(Bitmap bitmap) {
    // Convert it into byte array (required for tile creation)
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
    return new Tile(TILE_DIM, TILE_DIM, bitmapdata);
}
Also used : Tile(com.google.android.gms.maps.model.Tile) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with Tile

use of com.google.android.gms.maps.model.Tile in project collect by opendatakit.

the class GoogleMapsMapBoxOfflineTileProvider method getTile.

// ------------------------------------------------------------------------
// TileProvider Interface
// ------------------------------------------------------------------------
@Override
public Tile getTile(int x, int y, int z) {
    Tile tile = NO_TILE;
    if (this.isZoomLevelAvailable(z) && this.isDatabaseAvailable()) {
        String[] projection = { "tile_data" };
        int row = (int) (Math.pow(2, z) - y) - 1;
        String predicate = "tile_row = ? AND tile_column = ? AND zoom_level = ?";
        String[] values = { String.valueOf(row), String.valueOf(x), String.valueOf(z) };
        Cursor c = this.database.query("tiles", projection, predicate, values, null, null, null);
        if (c != null) {
            c.moveToFirst();
            if (!c.isAfterLast()) {
                tile = new Tile(256, 256, c.getBlob(0));
            }
            c.close();
        }
    }
    return tile;
}
Also used : Tile(com.google.android.gms.maps.model.Tile) Cursor(android.database.Cursor)

Aggregations

Tile (com.google.android.gms.maps.model.Tile)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Cursor (android.database.Cursor)2 IOException (java.io.IOException)2 SuppressLint (android.annotation.SuppressLint)1 SharedPreferences (android.content.SharedPreferences)1 MenuItem (android.view.MenuItem)1 ViewGroup (android.view.ViewGroup)1 RelativeLayout (android.widget.RelativeLayout)1 GoogleMap (com.google.android.gms.maps.GoogleMap)1 OnMapReadyCallback (com.google.android.gms.maps.OnMapReadyCallback)1 CameraPosition (com.google.android.gms.maps.model.CameraPosition)1 LatLng (com.google.android.gms.maps.model.LatLng)1 TileOverlayOptions (com.google.android.gms.maps.model.TileOverlayOptions)1 TileProvider (com.google.android.gms.maps.model.TileProvider)1 DiskLruCache (com.jakewharton.disklrucache.DiskLruCache)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1