Skip to content

Instantly share code, notes, and snippets.

@yusufceylan
Created September 23, 2020 09:05
Show Gist options
  • Save yusufceylan/3502b98ccea4346530e42078b7189800 to your computer and use it in GitHub Desktop.
Save yusufceylan/3502b98ccea4346530e42078b7189800 to your computer and use it in GitHub Desktop.
Basic Huawei Map
class MainActivity : AppCompatActivity(), OnMapReadyCallback {
private var hMap: HuaweiMap? = null
companion object {
private const val MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set Api Key and content
MapsInitializer.setApiKey("Your API Key");
setContentView(R.layout.activity_main)
// Init UI
initHuaweiMap(savedInstanceState)
}
/**
* Initialize the Huawei Map
*/
private fun initHuaweiMap(savedInstanceState: Bundle?) {
var mapViewBundle: Bundle? = null
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY)
}
map_view?.apply {
onCreate(mapViewBundle)
getMapAsync(this@MainActivity)
}
}
override fun onMapReady(map: HuaweiMap?) {
hMap = map
hMap?.isMyLocationEnabled = true // Enable the my-location overlay.
hMap?.uiSettings?.isMyLocationButtonEnabled = true // Enable the my-location icon.
}
override fun onStart() {
map_view?.onStart()
super.onStart()
}
override fun onStop() {
map_view?.onStop()
super.onStop()
}
override fun onDestroy() {
hMap?.clear()
hMap = null
map_view?.onDestroy()
super.onDestroy()
}
override fun onPause() {
map_view?.onPause()
super.onPause()
}
override fun onResume() {
map_view?.onResume()
super.onResume()
}
override fun onLowMemory() {
map_view?.onLowMemory()
super.onLowMemory()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
var mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY)
if (mapViewBundle == null) {
mapViewBundle = Bundle()
outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle)
}
map_view.onSaveInstanceState(mapViewBundle)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment