package com.mappls.sdk.direction.ui.react import android.app.Activity import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Color import android.util.Log import com.facebook.react.bridge.* import com.google.gson.Gson import com.mappls.sdk.direction.ui.model.DirectionOptions import com.mappls.sdk.direction.ui.model.DirectionPoint import com.mappls.sdk.geojson.Point import com.mappls.sdk.maps.Mappls import com.mappls.sdk.nearby.plugin.CategoryCode import com.mappls.sdk.plugins.places.autocomplete.model.MapplsFavoritePlace import com.mappls.sdk.plugins.places.autocomplete.model.PlaceOptions import com.mappls.sdk.services.api.autosuggest.AutoSuggestCriteria import com.mappls.sdk.services.api.directions.DirectionsCriteria import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import java.io.IOException import java.io.InputStream import java.net.HttpURLConnection import java.net.URL class MapplsDirectionWidgetReactNativeModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), ActivityEventListener { private lateinit var promise: Promise override fun getName(): String { return "MapplsDirectionWidgetReactNative" } override fun getConstants(): MutableMap { val constants: MutableMap = HashMap() //Resource constants["RESOURCE_ADV_KEY"] = DirectionsCriteria.RESOURCE_ROUTE; constants["RESOURCE_ETA_KEY"] = DirectionsCriteria.RESOURCE_ROUTE_ETA; constants["RESOURCE_TRAFFIC_KEY"] = DirectionsCriteria.RESOURCE_ROUTE_TRAFFIC; //Profile constants["PROFILE_DRIVING_KEY"] = DirectionsCriteria.PROFILE_DRIVING; constants["PROFILE_BIKING_KEY"] = DirectionsCriteria.PROFILE_BIKING; constants["PROFILE_TRUCKING_KEY"] = DirectionsCriteria.PROFILE_TRUCKING; constants["PROFILE_WALKING_KEY"] = DirectionsCriteria.PROFILE_WALKING; //Overview constants["OVERVIEW_FULL_KEY"] = DirectionsCriteria.OVERVIEW_FULL; constants["OVERVIEW_FALSE_KEY"] = DirectionsCriteria.OVERVIEW_FALSE; constants["OVERVIEW_SIMPLIFIED_KEY"] = DirectionsCriteria.OVERVIEW_SIMPLIFIED; //Exclude constants["EXCLUDE_TOLLS_KEY"] = DirectionsCriteria.EXCLUDE_TOLL; constants["EXCLUDE_FERRY_KEY"] = DirectionsCriteria.EXCLUDE_FERRY; constants["EXCLUDE_MOTORWAY_KEY"] = DirectionsCriteria.EXCLUDE_MOTORWAY; //annotations constants["ATTRIBUTIONS_CONGESTION_KEY"] = DirectionsCriteria.ANNOTATION_CONGESTION constants["ATTRIBUTIONS_DISTANCE_KEY"] = DirectionsCriteria.ANNOTATION_DISTANCE constants["ATTRIBUTIONS_DURATION_KEY"] = DirectionsCriteria.ANNOTATION_DURATION constants["ATTRIBUTIONS_MAXSPEED_KEY"] = DirectionsCriteria.ANNOTATION_MAXSPEED constants["ATTRIBUTIONS_SPEED_KEY"] = DirectionsCriteria.ANNOTATION_SPEED constants["ATTRIBUTIONS_SPEED_LIMIT_KEY"] = DirectionsCriteria.ANNOTATION_SPEED_LIMIT //PlaceOptions //Pods constants["POD_SUB_LOCALITY_KEY"] = AutoSuggestCriteria.POD_SUB_LOCALITY constants["POD_LOCALITY_KEY"] = AutoSuggestCriteria.POD_LOCALITY constants["POD_CITY_KEY"] = AutoSuggestCriteria.POD_CITY constants["POD_VILLAGE_KEY"] = AutoSuggestCriteria.POD_VILLAGE constants["POD_SUB_DISTRICT_KEY"] = AutoSuggestCriteria.POD_SUB_DISTRICT constants["POD_DISTRICT_KEY"] = AutoSuggestCriteria.POD_DISTRICT constants["POD_STATE_KEY"] = AutoSuggestCriteria.POD_STATE constants["POD_SUB_SUB_LOCALITY_KEY"] = AutoSuggestCriteria.POD_SUB_SUB_LOCALITY //attributionHorizontalAlignment constants["GRAVITY_LEFT_KEY"] = PlaceOptions.GRAVITY_LEFT constants["GRAVITY_CENTER_KEY"] = PlaceOptions.GRAVITY_CENTER constants["GRAVITY_RIGHT_KEY"] = PlaceOptions.GRAVITY_RIGHT //attributionVerticalAlignment constants["GRAVITY_TOP_KEY"] = PlaceOptions.GRAVITY_TOP constants["GRAVITY_BOTTOM_KEY"] = PlaceOptions.GRAVITY_BOTTOM //logoSize constants["SIZE_SMALL_KEY"] = PlaceOptions.SIZE_SMALL constants["SIZE_MEDIUM_KEY"] = PlaceOptions.SIZE_MEDIUM constants["SIZE_LARGE_KEY"] = PlaceOptions.SIZE_LARGE return constants } override fun initialize() { super.initialize() reactApplicationContext.addActivityEventListener(this) reactApplicationContext.runOnUiQueueThread { Mappls.getInstance(reactApplicationContext) } } @ReactMethod fun openDirectionWidget(map: ReadableMap, promise: Promise) { this.promise = promise MapplsDirectionSetting.getInstance().clear() if (reactApplicationContext.currentActivity == null) { promise.reject("Error", "You are not directly attached with activity"); return; } val builder = DirectionOptions.builder() if (map.hasKey("resource") && !map.isNull("resource")) { builder.resource(map.getString("resource")!!) } if (map.hasKey("showAlternative") && !map.isNull("showAlternative")) { builder.showAlternative(map.getBoolean("showAlternative")) } if (map.hasKey("profile") && !map.isNull("profile")) { builder.profile(map.getString("profile")!!) } if (map.hasKey("overview") && !map.isNull("overview")) { builder.overview(map.getString("overview")!!) } if (map.hasKey("steps") && !map.isNull("steps")) { builder.steps(map.getBoolean("steps")) } if(map.hasKey("attributions") && !map.isNull("attributions")){ val annotation = map.getArray("attributions") val annotationList:MutableList = annotation!!.toArrayList().toMutableList() as MutableList builder.annotation(annotationList) } if (map.hasKey("excludes") && !map.isNull("excludes")) { val excludes = map.getArray("excludes") val excludesList:MutableList = excludes!!.toArrayList().toMutableList() as MutableList builder.excludes(excludesList) } if (map.hasKey("showStartNavigation") && !map.isNull("showStartNavigation")) { builder.showStartNavigation(map.getBoolean("showStartNavigation")) } if (map.hasKey("source") && !map.isNull("source")) { val source = map.getMap("source") if (source != null && source.hasKey("latitude") && source.hasKey("longitude")) { val latitude = source.getDouble("latitude") val longitude = source.getDouble("longitude") var address: String? = "" if (source.hasKey("address") && !source.isNull("address")) { address = source.getString("address") } val name: String? = if (source.hasKey("name") && !source.isNull("name")) { source.getString("name") } else { "$latitude, $longitude" } val directionPoint: DirectionPoint = DirectionPoint.setDirection(Point.fromLngLat(longitude, latitude), name, address) builder.origin(directionPoint) } else if (source != null && source.hasKey("mapplsPin")) { val eloc = source.getString("mapplsPin") var address: String? = "" if (source.hasKey("address") && !source.isNull("address")) { address = source.getString("address") } val name: String? = if (source.hasKey("name") && !source.isNull("name")) { source.getString("name") } else { " " } val directionPoint: DirectionPoint = DirectionPoint.setDirection(eloc, name, address) builder.origin(directionPoint) } } if (map.hasKey("destination") && !map.isNull("destination")) { val destination = map.getMap("destination") if (destination != null && destination.hasKey("latitude") && destination.hasKey("longitude")) { val latitude = destination.getDouble("latitude") val longitude = destination.getDouble("longitude") var address: String? = "" if (destination.hasKey("address") && !destination.isNull("address")) { address = destination.getString("address") } val name: String? name = if (destination.hasKey("name") && !destination.isNull("name")) { destination.getString("name") } else { "$latitude, $longitude" } val directionPoint: DirectionPoint = DirectionPoint.setDirection(Point.fromLngLat(longitude, latitude), name, address) builder.destination(directionPoint) } else if (destination != null && destination.hasKey("mapplsPin")) { val eloc = destination.getString("mapplsPin") var address: String? = "" if (destination.hasKey("address") && !destination.isNull("address")) { address = destination.getString("address") } val name: String? name = if (destination.hasKey("name") && !destination.isNull("name")) { destination.getString("name") } else { " " } val directionPoint: DirectionPoint = DirectionPoint.setDirection(eloc, name, address) builder.destination(directionPoint) } } if (map.hasKey("searchAlongRoute") && !map.isNull("searchAlongRoute")){ val searchAlongRoute = map.getBoolean("searchAlongRoute") builder.searchAlongRoute(searchAlongRoute) } if (map.hasKey("alongRouteBuffer") && !map.isNull("alongRouteBuffer")){ val alongRouteBuffer = map.getInt("alongRouteBuffer") builder.alongRouteBuffer(alongRouteBuffer) } if (map.hasKey("categoryCodes") && !map.isNull("categoryCodes")) { val categoryCodeList: MutableList = ArrayList() setCategoryCodes(map, categoryCodeList) MapplsDirectionSetting.getInstance().categoryCodes = categoryCodeList } if(map.hasKey("showProfileOption") && !map.isNull("showProfileOption")) { val showProfileOption = map.getBoolean("showProfileOption") builder.showProfileOption(showProfileOption); } if(map.hasKey("selectedRouteColor") && !map.isNull("selectedRouteColor")) { val selectedRouteColor = map.getString("selectedRouteColor") builder.selectedRouteColor(Color.parseColor(selectedRouteColor)); } if(map.hasKey("selectedCasingRouteColor") && !map.isNull("selectedCasingRouteColor")) { val selectedCasingRouteColor = map.getString("selectedCasingRouteColor") builder.selectedCasingRouteColor(Color.parseColor(selectedCasingRouteColor)); } if(map.hasKey("alternateRouteColor") && !map.isNull("alternateRouteColor")) { val alternateRouteColor = map.getString("alternateRouteColor") builder.alternateRouteColor(Color.parseColor(alternateRouteColor)); } if(map.hasKey("alternateCasingRouteColor") && !map.isNull("alternateCasingRouteColor")) { val alternateCasingRouteColor = map.getString("alternateCasingRouteColor") builder.alternateCasingRouteColor(Color.parseColor(alternateCasingRouteColor)); } if(map.hasKey("showAddWaypointOption") && !map.isNull("showAddWaypointOption")) { val showAddWaypointOption = map.getBoolean("showAddWaypointOption") builder.showAddWaypointOption(showAddWaypointOption); } if(map.hasKey("showRouteReportSummary") && !map.isNull("showRouteReportSummary")) { val showRouteReportSummary = map.getBoolean("showRouteReportSummary") builder.showRouteReportSummary(showRouteReportSummary); } if(map.hasKey("showRouteReportSummaryOnMap") && !map.isNull("showRouteReportSummaryOnMap")) { val showRouteReportSummaryOnMap = map.getBoolean("showRouteReportSummaryOnMap") builder.showRouteReportSummaryOnMap(showRouteReportSummaryOnMap); } if(map.hasKey("showTripCostSummary") && !map.isNull("showTripCostSummary")) { val showTripCostSummary = map.getBoolean("showTripCostSummary") builder.showTripCostSummary(showTripCostSummary); } if(map.hasKey("isSort") && !map.isNull("isSort")) { val isSort = map.getBoolean("isSort") builder.isSort(isSort); } if(map.hasKey("routeType") && !map.isNull("routeType")) { val routeType = map.getInt("routeType") builder.routeType(routeType); } if(map.hasKey("lessVerbose") && !map.isNull("lessVerbose")) { val lessVerbose = map.getBoolean("lessVerbose") builder.lessVerbose(lessVerbose); } if(map.hasKey("continueStraight") && !map.isNull("continueStraight")) { val continueStraight = map.getBoolean("continueStraight") builder.continueStraight(continueStraight); } if(map.hasKey("geometries") && !map.isNull("geometries")) { val geometries = map.getString("geometries") builder.geometries(geometries!!); } if(map.hasKey("instructions") && !map.isNull("instructions")) { val instructions = map.getBoolean("instructions") builder.instructions(instructions); } if(map.hasKey("showHeavyVehicleProfile") && !map.isNull("showHeavyVehicleProfile")) { val showHeavyVehicleProfile = map.getBoolean("showHeavyVehicleProfile") builder.showHeavyVehicleProfile(showHeavyVehicleProfile); } if(map.hasKey("showWalkingProfile") && !map.isNull("showWalkingProfile")) { val showWalkingProfile = map.getBoolean("showWalkingProfile") builder.showWalkingProfile(showWalkingProfile); } if(map.hasKey("mapplsStyle") && !map.isNull("mapplsStyle")) { val mapplsStyle = map.getString("mapplsStyle") builder.mapplsStyle(mapplsStyle); } if (map.hasKey("searchPlaceOption") && !map.isNull("searchPlaceOption")) { val searchMap = map.getMap("searchPlaceOption")!! val placeOptionsBuilder = PlaceOptions.builder() if (searchMap.hasKey("location")) { val location=searchMap.getArray("location") //val latLong: List = searchMap.getString("location")!!.split(",") if (location!=null&&location.size()==2){ val latitude = location.getDouble(1)//latLong[1].toDouble() val longitude = location.getDouble(0)//latLong[0].toDouble() placeOptionsBuilder.location(Point.fromLngLat(longitude, latitude)) } } if (searchMap.hasKey("historyCount")) { val historyCount = searchMap.getInt("historyCount") placeOptionsBuilder.historyCount(historyCount) } if (searchMap.hasKey("saveHistory")) { val saveHistory = searchMap.getBoolean("saveHistory") placeOptionsBuilder.saveHistory(saveHistory) } if (searchMap.hasKey("pod")) { val pod = searchMap.getString("pod") placeOptionsBuilder.pod(pod) } if (searchMap.hasKey("backgroundColor")) { val backgroundColor: Int = Color.parseColor(searchMap.getString("backgroundColor")) placeOptionsBuilder.backgroundColor(backgroundColor) } else { placeOptionsBuilder.backgroundColor(Color.WHITE) } if (searchMap.hasKey("tokenizeAddress")) { val tokenizeAddress = searchMap.getBoolean("tokenizeAddress") if (tokenizeAddress) placeOptionsBuilder.tokenizeAddress(true) } if (searchMap.hasKey("toolbarColor")) { val toolbarColor: Int = Color.parseColor(searchMap.getString("toolbarColor")) placeOptionsBuilder.toolbarColor(toolbarColor) } if (searchMap.hasKey("hint")) { val hint = searchMap.getString("hint") placeOptionsBuilder.hint(hint) } if (searchMap.hasKey("filter")) { val filter = searchMap.getString("filter") placeOptionsBuilder.filter(filter) } if (searchMap.hasKey("attributionVerticalAlignment")) { val attributionVerticalAlignment = searchMap.getInt("attributionVerticalAlignment") placeOptionsBuilder.attributionHorizontalAlignment(attributionVerticalAlignment) } if (searchMap.hasKey("attributionHorizontalAlignment")) { val attributionHorizontalAlignment = searchMap.getInt("attributionHorizontalAlignment") placeOptionsBuilder.attributionVerticalAlignment(attributionHorizontalAlignment) } if (searchMap.hasKey("logoSize")) { val logoSize = searchMap.getInt("logoSize") placeOptionsBuilder.logoSize(logoSize) } if (searchMap.hasKey("userAddedLocationEnable") && !searchMap.isNull("userAddedLocationEnable")) { val userAddedLocationEnable = searchMap.getBoolean("userAddedLocationEnable") placeOptionsBuilder.userAddedLocationEnable(userAddedLocationEnable) } if (searchMap.hasKey("limit") && !searchMap.isNull("limit")) { val limit = searchMap.getInt("limit") placeOptionsBuilder.limit(limit) } if (searchMap.hasKey("enableTextSearch") && !searchMap.isNull("enableTextSearch")) { val enableTextSearch = searchMap.getBoolean("enableTextSearch") placeOptionsBuilder.enableTextSearch(enableTextSearch) } if (searchMap.hasKey("minCharactersForSearch") && !searchMap.isNull("minCharactersForSearch")) { val minCharactersForSearch = searchMap.getInt("minCharactersForSearch") placeOptionsBuilder.minCharactersForSearch(minCharactersForSearch) } if (searchMap.hasKey("statusBarColor") && !searchMap.isNull("statusBarColor")) { val statusBarColor = Color.parseColor(map.getString("statusBarColor")) placeOptionsBuilder.statusBarColor(statusBarColor) } if (searchMap.hasKey("toolbarTintColor") && !searchMap.isNull("toolbarTintColor")) { val toolbarTintColor = Color.parseColor(map.getString("toolbarTintColor")) placeOptionsBuilder.toolbarTintColor(toolbarTintColor) } if (searchMap.hasKey("showPoweredByText") && !searchMap.isNull("showPoweredByText")) { val showPoweredByText = searchMap.getBoolean("showPoweredByText") placeOptionsBuilder.showPoweredByText(showPoweredByText) } if (searchMap.hasKey("isShowCurrentLocation") && !searchMap.isNull("isShowCurrentLocation")) { val isShowCurrentLocation = searchMap.getBoolean("isShowCurrentLocation") placeOptionsBuilder.isShowCurrentLocation(isShowCurrentLocation) } if (searchMap.hasKey("debounce") && !searchMap.isNull("debounce")) { val debounce = searchMap.getInt("debounce") placeOptionsBuilder.debounce(debounce) } if (map.hasKey("favoritePlaces")) { val readableArray = map.getArray("favoritePlaces") if (readableArray != null) { val mapplsFavoritePlaces: MutableList = java.util.ArrayList() for (i in 0 until readableArray.size()) { val favorite = readableArray.getString(i) mapplsFavoritePlaces.add( Gson().fromJson( favorite, MapplsFavoritePlace::class.java ) ) } placeOptionsBuilder.favoritePlaces(mapplsFavoritePlaces) } } if (map.hasKey("favoriteCount")) { val count = map.getInt("favoriteCount") placeOptionsBuilder.favoriteCount(count) } if (map.hasKey("resultBackgroundColor")) { val backgroundColor = Color.parseColor(map.getString("resultBackgroundColor")) placeOptionsBuilder.resultBackgroundColor(backgroundColor) } if (map.hasKey("placeNameTextColor")) { val placeNameTextColor = Color.parseColor(map.getString("placeNameTextColor")) placeOptionsBuilder.placeNameTextColor(placeNameTextColor) } if (map.hasKey("addressTextColor")) { val addressTextColor = Color.parseColor(map.getString("addressTextColor")) placeOptionsBuilder.addressTextColor(addressTextColor) } if (map.hasKey("savedPlaceNameTextColor")) { val savedPlaceNameTextColor = Color.parseColor(map.getString("savedPlaceNameTextColor")) placeOptionsBuilder.savedPlaceNameTextColor(savedPlaceNameTextColor) } if (map.hasKey("favoritePlaceNameTextColor")) { val favoritePlaceNameTextColor = Color.parseColor(map.getString("favoritePlaceNameTextColor")) placeOptionsBuilder.favoritePlaceNameTextColor(favoritePlaceNameTextColor) } if (map.hasKey("distanceTextColor")) { val distanceTextColor = Color.parseColor(map.getString("distanceTextColor")) placeOptionsBuilder.distanceTextColor(distanceTextColor) } if (map.hasKey("errorBackgroundColor")) { val errorBackgroundColor = Color.parseColor(map.getString("errorBackgroundColor")) placeOptionsBuilder.errorBackgroundColor(errorBackgroundColor) } if (map.hasKey("errorTextColor")) { val errorTextColor = Color.parseColor(map.getString("errorTextColor")) placeOptionsBuilder.errorTextColor(errorTextColor) } if (map.hasKey("hintColor")) { val hintColor = Color.parseColor(map.getString("hintColor")) placeOptionsBuilder.hintColor(hintColor) } if (map.hasKey("poweredByTextColor")) { val poweredByTextColor = Color.parseColor(map.getString("poweredByTextColor")) placeOptionsBuilder.poweredByTextColor(poweredByTextColor) } if (map.hasKey("attributionBackgroundColor")) { val attributionBackgroundColor = Color.parseColor(map.getString("attributionBackgroundColor")) placeOptionsBuilder.attributionBackgroundColor(attributionBackgroundColor) } if (map.hasKey("currentLocationBackground")) { val currentLocationBackground = Color.parseColor(map.getString("currentLocationBackground")) placeOptionsBuilder.currentLocationBackground(currentLocationBackground) } if (map.hasKey("responseLang")) { val responseLang = map.getString("responseLang") placeOptionsBuilder.responseLang(responseLang) } builder.searchPlaceOption(placeOptionsBuilder.build()) } MapplsDirectionSetting.getInstance().directionOptions = builder val intent = Intent(reactApplicationContext, DirectionActivity::class.java) //intent.putExtra("builder", builder.build()); reactApplicationContext.currentActivity?.startActivityForResult(intent, 101) } override fun onNewIntent(intent: Intent) { } override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == 101) { if (resultCode == Activity.RESULT_OK) { // val gsonBuilder = GsonBuilder().create() // val origin = MapplsDirectionSetting.getInstance().origin // val destination = MapplsDirectionSetting.getInstance().destination val selectedIndex = MapplsDirectionSetting.getInstance().selectedIndex // val waypoints = MapplsDirectionSetting.getInstance().wayPoints val directionsResponse = MapplsDirectionSetting.getInstance().directionsResponse MapplsDirectionSetting.getInstance().clear() /* val waypointsArray = Arguments.createArray() if (waypoints != null && waypoints.isNotEmpty()) { for (point in waypoints) { waypointsArray.pushMap(jsonToReact(JSONObject(gsonBuilder.toJson(point)))) } }*/ val resultMap = Arguments.createMap() /* if (origin != null) { resultMap.putMap("origin", jsonToReact(JSONObject(gsonBuilder.toJson(origin)))) } if (destination != null) { resultMap.putMap("destination", jsonToReact(JSONObject(gsonBuilder.toJson(destination)))) }*/ resultMap.putInt("selectedIndex", selectedIndex ?: 0) /* if (waypointsArray.size() > 0) { resultMap.putArray("waypoints", waypointsArray) } */ if (directionsResponse != null) { resultMap.putMap("directionsResponse", jsonToReact(JSONObject(directionsResponse.toJson()))) } promise.resolve(resultMap) } else if (resultCode == Activity.RESULT_CANCELED) { promise.reject("Cancel", "Close") } } } @Throws(JSONException::class) fun jsonToReact(jsonObject: JSONObject): WritableMap? { val writableMap = Arguments.createMap() val iterator: Iterator<*> = jsonObject.keys() while (iterator.hasNext()) { val key = iterator.next() as String val value = jsonObject[key] if (value is Float || value is Double) { writableMap.putDouble(key, jsonObject.getDouble(key)) } else if (value is Number) { writableMap.putInt(key, jsonObject.getInt(key)) } else if (value is String) { writableMap.putString(key, jsonObject.getString(key)) } else if (value is JSONObject) { writableMap.putMap(key, jsonToReact(jsonObject.getJSONObject(key))) } else if (value is JSONArray) { writableMap.putArray(key, jsonToReact(jsonObject.getJSONArray(key))) } else if (value === JSONObject.NULL) { writableMap.putNull(key) } } return writableMap } @Throws(JSONException::class) fun jsonToReact(jsonArray: JSONArray): WritableArray? { val writableArray = Arguments.createArray() for (i in 0 until jsonArray.length()) { val value = jsonArray[i] if (value is Float || value is Double) { writableArray.pushDouble(jsonArray.getDouble(i)) } else if (value is Number) { writableArray.pushInt(jsonArray.getInt(i)) } else if (value is String) { writableArray.pushString(jsonArray.getString(i)) } else if (value is JSONObject) { writableArray.pushMap(jsonToReact(jsonArray.getJSONObject(i))) } else if (value is JSONArray) { writableArray.pushArray(jsonToReact(jsonArray.getJSONArray(i))) } else if (value === JSONObject.NULL) { writableArray.pushNull() } } return writableArray } private fun setCategoryCodes(map: ReadableMap, categoryCodeList: MutableList) { val categoryCodesArray = map.getArray("categoryCodes") for (i in 0 until categoryCodesArray!!.size()) { val categoryCodeMap = categoryCodesArray.getMap(i) val categoryName = categoryCodeMap?.getString("category") val categoryCodeReadableArray = categoryCodeMap?.getArray("categoryCode") val isSelected =if (categoryCodeMap?.hasKey("isSelected") == true && !categoryCodeMap.isNull("isSelected")) categoryCodeMap.getBoolean("isSelected") else false val categoryCode: MutableList = ArrayList() for (j in 0 until categoryCodeReadableArray!!.size()) { categoryCodeReadableArray.getString(j)?.let { categoryCode.add(it) } } val iconImageMap = categoryCodeMap.getMap("icon") val iconUrl = iconImageMap!!.getString("uri") val markerIconMap = categoryCodeMap.getMap("markerIcon") val markerIconUrl = markerIconMap!!.getString("uri") val icon = getBitmapFromURL(iconUrl) val markerIcon = getBitmapFromURL(markerIconUrl) categoryCodeList.add( CategoryCode( categoryName, icon, categoryCode, markerIcon,isSelected ) ) } } private fun getBitmapFromURL(src: String?): Bitmap? { return try { val url = URL(src) val connection: HttpURLConnection = url.openConnection() as HttpURLConnection connection.doInput = true connection.connect() val input: InputStream = connection.inputStream BitmapFactory.decodeStream(input) } catch (e: IOException) { // Log exception Log.e("Error", "Bitmap decoding error: " + e.localizedMessage) null } } }