大佬教程收集整理的这篇文章主要介绍了Kotlin 中 AndroidStudio 中 GoogleMap 底部的简单 Windows,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在为我的大学科目之一开发 AndroID 应用。在这一点上,我有一个带有多个标记的 GoogleMaps 地图,用于标记感兴趣的位置。那真的没有关系。每当我单击任何标记时,我都会尝试弹出一个窗口。该窗口应该填充屏幕的下三分之一并包含有关标记位置的信息。我已经在地图上添加了一个 OnMarkerClickListener,每当我按下一个标记时就会调用 onMarkerClick() 方法,但我真的不知道如何设置这样一个窗口。我知道 AndroIDstudio 有一种场景构建器,我可以用它来设计窗口,但是我如何显示它以及如何控制它在屏幕上的弹出位置?我希望有人可以帮助我。我也是 Kotlin 的新手。过去我用Java做了很多。 这是应用当前状态的链接:
@R_489_10107@s://imgur.com/00sZF7o
这是我的 MapsActivity.kt 代码:
package com.example.multimodaltraffic
import androID.Manifest
import androID.content.pm.PackageManager
import androID.os.bundle
import androID.Widget.Toast
import androIDx.appcompat.app.AppCompatActivity
import androIDx.core.app.ActivityCompat
import androIDx.core.content.ContextCompat
import com.example.multimodaltraffic.API.APIrequests
import com.example.multimodaltraffic.API.stations.NetworkJson
import com.Google.androID.gms.maps.CameraupdateFactory
import com.Google.androID.gms.maps.GoogleMap
import com.Google.androID.gms.maps.OnMapReadyCallBACk
import com.Google.androID.gms.maps.SupportMapFragment
import com.Google.androID.gms.maps.model.bitmapDescriptorFactory
import com.Google.androID.gms.maps.model.LatLng
import com.Google.androID.gms.maps.model.Marker
import com.Google.androID.gms.maps.model.MarkerOptions
import kotlinx.coroutInes.dispatchers
import kotlinx.coroutInes.GlobalScope
import kotlinx.coroutInes.launch
import kotlinx.coroutInes.withContext
import retrofit2.Retrofit
import retrofit2.awaitResponse
import retrofit2.converter.gson.GsonConverterFactory
const val BASE_URL = "@R_489_10107@s://API.citybik.es"
class MapsActivity : AppCompatActivity(),OnMapReadyCallBACk,GoogleMap.onMarkerClickListener {
private lateinit var mMap: GoogleMap
private val LOCATION_PERMISSION_requEST = 1
private fun getLOCATIOnAccess() {
if (ContextCompat.checkSelfPermission(this,androID.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLOCATIOnEnabled = true
}
else
ActivityCompat.requestPermissions(this,arrayOf(androID.Manifest.permission.ACCESS_FINE_LOCATION),LOCATION_PERMISSION_requEST)
}
overrIDe fun onrequestPermissionsResult(requestCode: Int,permissions: Array<String>,grantResults: IntArray) {
if (requestCode == LOCATION_PERMISSION_requEST) {
if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Todo: ConsIDer calling
// ActivityCompat#requestPermissions
// here to request the missing permissions,and then overrIDing
// public voID onrequestPermissionsResult(int requestCode,String[] permissions,// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return
}
mMap.isMyLOCATIOnEnabled = true
}
else {
Toast.makeText(this,"User has not granted LOCATIOn access permission",Toast.LENGTH_LONG).show()
finish()
}
}
}
overrIDe fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceStatE)
setContentVIEw(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notifIEd when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentByID(R.ID.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
/**
* Manipulates the map once available.
* This callBACk is triggered when the map is ready to be used.
* This is where we can add markers or lines,add Listeners or move the camera. In this case,*/
overrIDe fun onMapReady(GoogleMap: GoogleMap) {
mMap = GoogleMap
getLOCATIOnAccess()
// Placeholder for camera start position
// @Todo change to current position
val kassel =LatLng(51.313139,9.465458)
mMap.moveCamera(CameraupdateFactory.newLatLngZoom(kassel,8f))
mMap.setonMarkerClickListener(this)
// current zoom level
/*
mMap.setonCameraIDleListener {
Log.d("DEBUG",mMap.cameraposition.zoom.toString())
}
*/
getCurrentData()
}
/**
* Get data from CityBikes API
* First all networks,then all stations of the networks
* Calls function to add markers on map
* */
private fun getCurrentData() {
var API = Retrofit.builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(APIrequests::class.java)
GlobalScope.launch(dispatchers.IO) {
try {
val response = API.getNextbikeData().awaitResponse()
if (response.issuccessful) {
var networks = response.body()!!
networks.networks.forEach{
if(it.LOCATIOn.country == "DE"){
GlobalScope.launch(dispatchers.IO) {
API = Retrofit.builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(APIrequests::class.java)
val response = API.getStations(it.ID).awaitResponse()
if (response.issuccessful) {
var network = response.body()!!
setStationMarker(network)
}
}
}
}
}
} catch (e: Exception) {
withContext(dispatchers.Main){
Toast.makeText(
applicationContext,"Nextbike networks Could not be loaded.",Toast.LENGTH_SHORT
).show()
}
}
}
}
// Add marker for each Nextbike station
private fun setStationMarker(network: NetworkJson){
// @Todo Safe marker to update informations
network.network.stations.forEach{
val station = LatLng(it.latitude,it.longitudE)
this@mapsActivity.runOnUiThread(Runnable {
if (it.freeBikes > 0) {
mMap.addMarker(MarkerOptions()
.position(station)
.title(it.Name)
.snippet("Free bikes: " + it.freeBikes)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
)
} else {
mMap.addMarker(MarkerOptions()
.position(station)
.title(it.Name)
.snippet("Free bikes: " + it.freeBikes)
)
}
})
}
}
overrIDe fun onMarkerClick(p0: Marker?): Boolean {
if (p0 != null) {
p0.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
}
return true;
}
}
这是我的 maps_activity.xml 的代码:
<?xml version="1.0" enCoding="utf-8"?>
<fragment xmlns:androID="@R_489_10107@://scheR_301_11845@as.androID.com/apk/res/androID"
xmlns:map="@R_489_10107@://scheR_301_11845@as.androID.com/apk/res-auto"
xmlns:tools="@R_489_10107@://scheR_301_11845@as.androID.com/tools"
androID:ID="@+ID/map"
androID:name="com.Google.androID.gms.maps.SupportMapFragment"
androID:layout_wIDth="match_parent"
androID:layout_height="match_parent"
tools:context=".MapsActivity" />
它被称为BottomSheetDialogFragment。
这里有一些参考 MindOrks Tutorial 和 Medium
这是我的建议
创建 Fragment 首先,我将其命名为 BottomDialogFragment
将 Fragment() 更改为 BottomSheetDialogFragment()
class BottomDialogFragment : Fragment()
class BottomDialogFragment : BottomSheetDialogFragment()
设计您的布局
将内部BottomDialogFragment改成
class BottomDialogFragment : BottomSheetDialogFragment() {
companion object {
val TAG : String = BottomDialogFragment::class.java.simplename
}
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.bottom_dialog_fragment,container,falsE)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceStatE)
//do whatever you want in here
//e.g: Handle Button Click etc
}
}
override fun onMarkerClick(p0: Marker?): Boolean {
if (p0 != null) {
p0.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
//call it like this
BottomDialogFragment.newInstance().apply {
show(supportFragmentManager,BottomDialogFragment.TAG)
}
}
return true;
}
以上是大佬教程为你收集整理的Kotlin 中 AndroidStudio 中 GoogleMap 底部的简单 Windows全部内容,希望文章能够帮你解决Kotlin 中 AndroidStudio 中 GoogleMap 底部的简单 Windows所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。