[英]Kotlin: how to get a latitude and longitude to call google map with intent?
i have a code like this 我有这样的代码
//onCreate()
btnmap.setOnClickListener{
viewMap()
}
//end of onCreate()
var MyLongitude:Double? = null
var Mylatitude:Double? = null
fun location(){
var locationListener = object : LocationListener {
override fun onLocationChanged(locationn: Location?) {
Mylongitude = locationn!!.longitude //i want to take this value
Mylatitude = locationn.latitude //i want to take this value
}
....
}
}
fun viewMap(){
Log.i(LOG, "$MyLongitude, $Mylatitude") //the output is null, null
val gmmIntentUri = Uri.parse("google.streetview:cbll=$MyLatitude,$MyLongitude")
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent)
}
i wanna call a google map with intent to know where is my location now, to do that i need a value of latitude and longitude in location(), if you see my code when i call in Log MyLatitude and MyLongitude in viewMap() the output is always null even though it has been given a value in location() before 我想打电话给谷歌地图,目的是想知道我现在的位置,要在location()中需要一个经度和纬度值,如果当我在viewMap()中的Log MyLatitude和MyLongitude中调用时看到了我的代码,即使输出之前在location()中已赋值,输出也始终为null
i've try to call viewMap() inside onLocationChanged() in location() and i get the value of latitude and longitude, but that is makes my app always call google map automatically every time the location changed 我尝试在location()的onLocationChanged()内调用viewMap(),并得到纬度和经度的值,但这使我的应用程序每次位置更改时总是自动调用google map
normally my app will call google map when i press the button btnmap 通常,当我按下btnmap按钮时,我的应用会调用google map
so, how to take a value of latitude and longitude from location() function for the intent in viewMap()? 那么,如何从location()函数中获取viewMap()中的意图的纬度和经度值?
You can learn more about user location from here . 您可以从此处了解有关用户位置的更多信息。 To get current location you have to ask user permission and define it in your
AndroidManifest.xml
file: 要获取当前位置,您必须征得用户许可并在您的
AndroidManifest.xml
文件中对其进行定义:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
In your Activity's onCreate()
method define the following: 在您的
Activity's onCreate()
方法中,定义以下内容:
val permissionState = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
if (permissionState != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), REQUEST_PERMISSIONS_REQUEST_CODE)
}
and override onRequestPermissionsResult
function like here: 并重写
onRequestPermissionsResult
函数,如下所示:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_PERMISSIONS_REQUEST_CODE -> {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
getLocation()
}
}
}
So, that was the main part of getting user location. 因此,那是获取用户位置的主要部分。 For the simplicity here is full code:
为了简单起见,下面是完整的代码:
class MainActivity : AppCompatActivity() {
companion object {
private const val REQUEST_PERMISSIONS_REQUEST_CODE = 1
}
private lateinit var txtView: TextView
private lateinit var fusedLocationClient: FusedLocationProviderClient
private var myLongitude: Double? = null
private var myLatitude: Double? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
txtView = findViewById(R.id.txtView)
txtView.setOnClickListener {
viewMap()
}
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
val permissionState = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
if (permissionState != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), REQUEST_PERMISSIONS_REQUEST_CODE)
}
if (myLatitude == null || myLatitude == null)
getLocation()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_PERMISSIONS_REQUEST_CODE -> {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
getLocation()
}
}
}
private fun viewMap() {
Log.i("LOG", "$myLongitude, $myLatitude")
val gmmIntentUri = Uri.parse("google.streetview:cbll=$myLatitude,$myLongitude")
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent)
}
@SuppressLint("MissingPermission")
private fun getLocation() {
fusedLocationClient.lastLocation.addOnSuccessListener { location : Location? ->
location?.let {
myLongitude = it.longitude
myLatitude = it.latitude
}
}
}
}
For more detailed information about user location just refer to the link above, there are a lot of learning materials. 有关用户位置的更多详细信息,请参考上面的链接,其中有很多学习资料。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.