Skip to content

Instantly share code, notes, and snippets.

@ljcamargo
Last active September 21, 2020 07:24
Show Gist options
  • Select an option

  • Save ljcamargo/c235d1dee781ef93ce271d75670835f4 to your computer and use it in GitHub Desktop.

Select an option

Save ljcamargo/c235d1dee781ef93ce271d75670835f4 to your computer and use it in GitHub Desktop.
package com.company.maps
import androidx.fragment.app.FragmentActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.GoogleMap as Map
class AgnosticMap {
private var map: Map? = null
fun startMap(
activity: FragmentActivity,
fragmentId: Int,
latitude: Double,
longitude: Double,
zoom: Float,
marker: Boolean? = true,
ready: (()->Unit)? = null
) {
val fragment = activity.supportFragmentManager.findFragmentById(fragmentId)
(fragment as? SupportMapFragment)?.apply {
getMapAsync { map ->
this@AgnosticMap.map = map
val point = LatLng(latitude, longitude)
map.apply {
uiSettings.isScrollGesturesEnabled = false
uiSettings.isZoomControlsEnabled = false
uiSettings.isCompassEnabled = false
moveCamera(CameraUpdateFactory.newLatLngZoom(point, zoom))
if (marker == true) addMarker(MarkerOptions().position(point))
}
ready?.invoke()
}
}
}
fun setOnMapClickListener(action: ()->Unit) {
map?.setOnMapClickListener { action() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment