NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


gmap.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/google_map_dynamic"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:mapType="normal"
map:uiCompass="true"
map:cameraTargetLat="24.629463"
map:cameraTargetLng="46.715905"
map:cameraZoom="16"
/>

hmap.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/huawei_map_dynamic"
class="com.huawei.hms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="24.629463"
map:cameraTargetLng="46.715905"
map:cameraZoom="16"
map:mapType="normal"
map:uiCompass="true"/>

code:

class MapActivity : AppCompatActivity(){
val TAG = "map_activity"
lateinit var locationManagerHuawei: HuaweiLocationManager
lateinit var locationManagerGoogle: GoogleLocationManager
lateinit var googleMap: GoogleMap
lateinit var huaweiMap: HuaweiMap

var myLocation: Location? = null

val PERMISSIONS = arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_map)

if (!hasLocationPermission()) requestPermission()

if (isOnlyHms(applicationContext)) {
locationManagerHuawei = HuaweiLocationManager(applicationContext)
// handleHuaweiLocation()
createAsH()
} else {
locationManagerGoogle = GoogleLocationManager(applicationContext)
// handleGoogleLocation()
createAsG()
}
}

fun createAsH() {
val inflatedLayout = layoutInflater.inflate(
R.layout.hmap,
findViewById(R.id.mapGroup),
false
)
findViewById<FrameLayout>(R.id.mapGroup).addView(inflatedLayout)
val map: com.huawei.hms.maps.SupportMapFragment? =
supportFragmentManager.findFragmentById(R.id.huawei_map_dynamic) as? com.huawei.hms.maps.SupportMapFragment
map?.getMapAsync {
huaweiMap = it
if (isGPSOpen(applicationContext)) {
huaweiMap.setMyLocationEnabled(true)
huaweiMap.getUiSettings().setMyLocationButtonEnabled(true)
locationAnimation()
} else {
huaweiMap.setMyLocationEnabled(false)
huaweiMap.getUiSettings().setMyLocationButtonEnabled(false)
handleHuaweiLocation()
}
}
}

@SuppressLint("MissingPermission")
fun createAsG() {
val inflatedLayout = layoutInflater.inflate(
R.layout.gmap,
findViewById(R.id.mapGroup),
false
)
findViewById<FrameLayout>(R.id.mapGroup).addView(inflatedLayout)
val map: SupportMapFragment? =
supportFragmentManager.findFragmentById(R.id.google_map_dynamic) as? SupportMapFragment
map?.getMapAsync {
googleMap = it
if (isGPSOpen(applicationContext)) {
googleMap.setMyLocationEnabled(true)
googleMap.getUiSettings().setMyLocationButtonEnabled(true)
locationAnimation()
} else {
googleMap.setMyLocationEnabled(false)
googleMap.getUiSettings().setMyLocationButtonEnabled(false)
handleGoogleLocation()
}
}
}

/**
* Determine whether to turn on GPS
*/
private fun isGPSOpen(context: Context): Boolean {
val locationManager = context.getSystemService(LOCATION_SERVICE) as LocationManager
val gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
val network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
return if (gps || network) {
true
} else {
false
}
}

/**
* Determine if you have the location permission
*/
private fun hasLocationPermission(): Boolean {
for (permission in PERMISSIONS) {
if (ActivityCompat.checkSelfPermission(
this,
permission
) != PackageManager.PERMISSION_GRANTED
) {
return false
}
}
return true
}

fun requestPermission() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
Log.i(TAG, "sdk < 28 Q")
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
||
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
val strings = arrayOf<String>(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
ActivityCompat.requestPermissions(this, strings, 1)
}
} else {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
||
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
||
ActivityCompat.checkSelfPermission(
this,
"android.permission.ACCESS_BACKGROUND_LOCATION"
) != PackageManager.PERMISSION_GRANTED
) {
val strings = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
"android.permission.ACCESS_BACKGROUND_LOCATION"
)
ActivityCompat.requestPermissions(this, strings, 1)
}
}
}

fun handleHuaweiLocation() {
locationManagerHuawei.checkLocationSettingsAndShowPopup(
this,
onSuccess =
{
locationManagerHuawei.getLastKnownLocation(
onSuccess = {
if (it == null) {
locationManagerHuawei.registerLocationUpdates(
onSuccess = {
val location: Location? = it
myLocation = location
printLocation(location)
})
} else {
printLocation(it)
}
},
onFail = null
)
}
)
}

fun handleGoogleLocation() {
locationManagerGoogle.checkLocationSettingsAndShowPopup(
this,
onSuccess =
{
locationManagerGoogle.getLastKnownLocation(
onSuccess = {
if (it == null) {
locationManagerGoogle.registerLocationUpdates(onSuccess = {
val location: Location? = it
myLocation = location
printLocation(location)
})
} else {
locationAnimation()
printLocation(it)
}
},
onFail = null
)
}
)
}

fun printLocation(location: Location?) {
Log.i(
TAG,
"Fetched Location: latitude: ${location?.latitude} | longitude: ${location?.longitude}"
)
}

fun locationAnimation() {

if (isOnlyHms(applicationContext)) {
locationManagerHuawei.getLastKnownLocation(onSuccess = {
if(it != null){
huaweiMap.moveCamera(
com.huawei.hms.maps.CameraUpdateFactory.newLatLng(
com.huawei.hms.maps.model.LatLng(
it.getLatitude(),
it.getLongitude()
)
)
)
huaweiMap.animateCamera(com.huawei.hms.maps.CameraUpdateFactory.zoomTo(18f), 2000, null)
}
})

} else {
locationManagerGoogle.getLastKnownLocation(onSuccess = {
if(it != null){
googleMap.moveCamera(
CameraUpdateFactory.newLatLng(
LatLng(
it.getLatitude(),
it.getLongitude()
)
)
)
googleMap.animateCamera(CameraUpdateFactory.zoomTo(18f), 2000, null)
}
})
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == HuaweiLocationManager.REQUEST_CHECK_SETTINGS) {
when (resultCode) {
Activity.RESULT_OK -> {
Log.d(TAG, "User confirm to access location")

if (isOnlyHms(applicationContext)) {
handleHuaweiLocation()
} else {
handleGoogleLocation()
}
}
Activity.RESULT_CANCELED -> {
Log.d(TAG, "User denied to access location")
}
}
}
}

override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 1) {
for (i in permissions.indices) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
if (isOnlyHms(applicationContext)) {
handleHuaweiLocation()
} else {
handleGoogleLocation()
}
}
}
}
}


}





     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.