Issue
I am learning how to deal with MapsFragment in Kotlin. So I am considering an example app consists of the followings:
- MainActivity = having one button and one frameLayout
- MapsFragment.
As you may guess, the MapsFragment is set to the frameLayout in MainActivity by the following way (this is a part of code in my MainActivity.kt
):
var mapsFragment: MapsFragment
mapsFragment = MapsFragment()
val transaction = supportFragmentManager.beginTransaction()
transaction.add(R.id.frameLayout, mapsFragment)
transaction.commit()
Now, what I am trying to implement as an exercise is the following: when one clicks the button, then a new marker appears in the (google) map at the current location. For this purpose, I implement a function getting the current location information called getLastKnownLocation(context: Context)
.
As it is explained in the automatically created comment in the MapsFragment.kt
file, markers are added to the map by using private val callback = OnMapReadyCallback
, which is declared in MapsFragment.kt
. But, for me, it seems like that there is no way to modify the variable callback
in MainActivity.kt
. I am not sure if this is the correct design of mechanism, but as a newcomer in this field, I naively guessed that I am required to implement the following procedure (1) button click, (2) get the current location, (3) location is delivered to callback
, (4) new marker appears for my purpose. But I could not figure out how to achieve the steps (3) and (4).
Could anyone give any tips for my (self) exercise? If there is better way to implement my exercise program through a procedure other than my naively guessed procedure, it would also be greatly helpful.
[Added] I should clarify what I want a bit more. Let us say that there are already N number of makers in the map. I want to add one more marker by clicking the button (previous N markers are still there).
Solution
This is covered very clearly here, you need to modify your Activity
to implement the OnMapReadyCallback
interface
, this will require you to override
onMapReady
function, which you can do as following
// Declare a property in your class to keep a reference to map object
private lateinit var mMap: GoogleMap
// This will be called when your map is ready
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions()
.position(sydney)
.title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
Now simply register this callback
on your map fragment
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
As to how to get last known location and set it on map
// Declare a propery to keep a reference to FusedLocationProviderClient
private lateinit var fusedLocationClient: FusedLocationProviderClient
// In your Activity onCreate method do fusedLocationClient init as
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
// Then register a callback to receive last known location, call it in the end of onMapReady
private fun getLastLocation(){
fusedLocationClient.lastLocation
.addOnSuccessListener { location : Location? ->
location?.let{
val currentLocation = LatLng(location.latitude, location.latitude)
mMap.addMarker(MarkerOptions()
.position(currentLocation )
.title("Me"))
}
}
}
Since you want to set the location on map, only call getLastLocation
function in the end of onMapReady
function(so that map is ready, when we try to set a marker)
Answered By - mightyWOZ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.