第一次制作应用程序,请耐心等待。我想获取保存在Realm中的位置并将其添加到带有标记的Google地图中。我已经使用SQLlite看到了一些,但是每次打开时我都需要对其进行更新。我正在使用kotlin并坚持在Google提供的MapsActivity上写些什么
object BikeDao {
fun getBikes() : List<Bike> {
val realm = Realm.getDefaultInstance()
return realm.where<Bike>().findAll().toList()
}
fun getAvailableBikes() : List<Bike> {
val realm = Realm.getDefaultInstance()
return realm.where<Bike>().equalTo("available", true).findAll()
}
fun addBike(bike : Bike) : Bike {
val realm = Realm.getDefaultInstance()
val index = realm.where<Bike>().count()
realm.executeTransaction {
val newBike = it.createObject<Bike>(index)
newBike.name = bike.name
newBike.type = bike.type
newBike.location = bike.location
newBike.hourlyPrice = bike.hourlyPrice
newBike.picture = bike.picture
newBike.available = bike.available
}
bike.id = index
return bike
}
关注要标记的可用自行车
Google的代码
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
private val bikeList = BikeDao.getBikes()
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)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
/*
val Copenhagen = LatLng(55.6761, 12.5683)
mMap.addMarker(MarkerOptions().position(Copenhagen).title("Marker in Copenhagen"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(Copenhagen))
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(Copenhagen,12f))
*/
}
}