private MarkerOptions createMarker(ReadableMap markerJson) { MarkerOptions options = new MarkerOptions(); options.position( new LatLng( markerJson.getMap("coordinates").getDouble("lat"), markerJson.getMap("coordinates").getDouble("lng"))); if (markerJson.hasKey("title")) { options.title(markerJson.getString("title")); } if (markerJson.hasKey("color")) { options.icon(BitmapDescriptorFactory.defaultMarker((float) markerJson.getDouble("color"))); } if (markerJson.hasKey("snippet")) { options.snippet(markerJson.getString("snippet")); } if (markerJson.hasKey("icon")) { String varName = ""; ReadableType iconType = markerJson.getType("icon"); if (iconType.compareTo(ReadableType.Map) >= 0) { ReadableMap icon = markerJson.getMap("icon"); try { int resId = getResourceDrawableId(icon.getString("uri")); Bitmap image = BitmapFactory.decodeResource(reactContext.getResources(), resId); options.icon( BitmapDescriptorFactory.fromBitmap( Bitmap.createScaledBitmap( image, icon.getInt("width"), icon.getInt("height"), true))); } catch (Exception e) { varName = icon.getString("uri"); } } else if (iconType.compareTo(ReadableType.String) >= 0) { varName = markerJson.getString("icon"); } if (!varName.equals("")) { // Changing marker icon to use resource int resourceValue = getResourceDrawableId(varName); Log.i(REACT_CLASS, varName + markerJson.toString()); options.icon(BitmapDescriptorFactory.fromResource(resourceValue)); } } if (markerJson.hasKey("anchor")) { ReadableArray anchor = markerJson.getArray("anchor"); options.anchor((float) anchor.getDouble(0), (float) anchor.getDouble(1)); } return options; }
/** * Converts a react native readable map into a JSON object. * * @param readableMap * @return */ @Nullable private JSONObject readableMapToJson(ReadableMap readableMap) { JSONObject jsonObject = new JSONObject(); if (readableMap == null) { return null; } ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); if (!iterator.hasNextKey()) { return null; } while (iterator.hasNextKey()) { String key = iterator.nextKey(); ReadableType readableType = readableMap.getType(key); try { switch (readableType) { case Null: jsonObject.put(key, null); break; case Boolean: jsonObject.put(key, readableMap.getBoolean(key)); break; case Number: // Can be int or double. jsonObject.put(key, readableMap.getDouble(key)); break; case String: jsonObject.put(key, readableMap.getString(key)); break; case Map: jsonObject.put(key, this.readableMapToJson(readableMap.getMap(key))); break; case Array: jsonObject.put(key, readableMap.getArray(key)); default: // Do nothing and fail silently } } catch (JSONException ex) { // Do nothing and fail silently } } return jsonObject; }
public ShowOptions(@Nullable ReadableMap options) { if (options == null) { return; } if (options.hasKey(CLOSABLE_KEY)) { closable = options.getBoolean(CLOSABLE_KEY); Log.d(TAG, CLOSABLE_KEY + closable); } if (options.hasKey(USE_MAGIC_LINK_KEY)) { useMagicLink = options.getBoolean(USE_MAGIC_LINK_KEY); Log.d(TAG, USE_MAGIC_LINK_KEY + useMagicLink); } if (options.hasKey(AUTH_PARAMS_KEY)) { ReadableMap reactMap = options.getMap(AUTH_PARAMS_KEY); authParams = OptionsHelper.convertReadableMapToMap(reactMap); Log.d(TAG, AUTH_PARAMS_KEY + authParams); } if (options.hasKey(CONNECTIONS_KEY)) { ReadableArray connections = options.getArray(CONNECTIONS_KEY); List<String> list = new ArrayList<>(connections.size()); for (int i = 0; i < connections.size(); i++) { String connectionName = connections.getString(i); switch (connectionName) { case LockReactModule.CONNECTION_EMAIL: connectionType = LockReactModule.CONNECTION_EMAIL; break; case LockReactModule.CONNECTION_SMS: connectionType = LockReactModule.CONNECTION_SMS; break; } list.add(connectionName); } this.connections = new String[list.size()]; this.connections = list.toArray(this.connections); Log.d(TAG, CONNECTIONS_KEY + list); } }