Hii i have a doubt in maps fragment. i have 8 markers to display in maps. i have done it like this.
This code is working perfectly fine. but i want to implement MVVM in this and i want keep all the location addresses in array list . when i have done that am getting null object reference in getLocationfromaddress() method at this line
val coder: Geocoder = Geocoder(context)
how to place all locations in arraylist
This code is working perfectly fine. but i want to implement MVVM in this and i want keep all the location addresses in array list . when i have done that am getting null object reference in getLocationfromaddress() method at this line
val coder: Geocoder = Geocoder(context)
how to place all locations in arraylist
Code:
class MapsActivity :AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener
{
private lateinit var mMap: GoogleMap
private lateinit var button_res: Button
lateinit var address: LatLng
lateinit var pietrasanta: LatLng
lateinit var Ikea_alexandra: LatLng
lateinit var candlenut_resturant: LatLng
lateinit var MDIS_Dhoby: LatLng
lateinit var ridges: LatLng
lateinit var hilton: LatLng
lateinit var library: LatLng
lateinit var tanglin: LatLng
companion object {
private val MY_PERMISSION_FINE_LOCATION = 101
}
@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
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,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
address = getLocationFromAddress( this, "ADDRESS 1 " ) //this will be main marker and in the middle of the maps activity
mMap.addMarker(
MarkerOptions().position(address/*LatLng(17.373838, 78.533271)*/).title("MDIS")
.snippet("501 Stirling Rd,Singapore 148951")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon))
)
.showInfoWindow()
mMap.moveCamera(CameraUpdateFactory.newLatLng(address))
mMap.animateCamera(CameraUpdateFactory.zoomIn())
mMap.animateCamera(CameraUpdateFactory.zoomTo(15F), 2000, null)
mMap.uiSettings.setAllGesturesEnabled(true)
mMap.uiSettings.isCompassEnabled = true
//the following markers are the nerby markers for address marker
pietrasanta= getLocationFromAddress(this,"ADDRESS 2")
Ikea_alexandra= getLocationFromAddress(this,"ADDRESS 3")
candlenut_resturant = getLocationFromAddress(this, "ADDRESS 4")
MDIS_Dhoby= getLocationFromAddress(this, " ADDRESS 5 ")
ridges = getLocationFromAddress(this, "ADDRESS 6")
hilton = getLocationFromAddress(this, "ADDRESS 7")
library= getLocationFromAddress(this, "ADDRESS 8")
tanglin = getLocationFromAddress(this, "ADDRESS 9")
mMap.addMarker(MarkerOptions().position(pietrasanta).title("Pietrasanta the Italian Resturant"))
mMap.addMarker(MarkerOptions().position(Ikea_alexandra).title("Ikea alexandra resturant"))
mMap.addMarker(MarkerOptions().position(candlenut_resturant).title("Candlenut Resturant"))
mMap.addMarker(MarkerOptions().position(MDIS_Dhoby).title("MDIS Dhoby Ghaut"))
mMap.addMarker(MarkerOptions().position(ridges).title("Southern Ridges"))
mMap.addMarker(MarkerOptions().position(hilton).title("Hilton Singapore"))
mMap.addMarker(MarkerOptions().position(library).title("Queenstown Public Library"))
mMap.addMarker(MarkerOptions().position(tanglin).title("Tanglin Halt Food Centre"))
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
mMap.isMyLocationEnabled = true
} else {//condition for Marshmello and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
MY_PERMISSION_FINE_LOCATION
)
}
}
fun getLocationFromAddress(context: Context, strAddress: String): LatLng {
val coder: Geocoder = Geocoder(context)
val address1: List<Address>
lateinit var p1: LatLng
try {
address1 = coder.getFromLocationName(strAddress, 5)
if (address1 == null) {
}
val location: Address = address1.get(0)
location.latitude
location.latitude
p1 = LatLng(location.latitude, location.longitude)
} catch (e: Exception) {
e.printStackTrace()
}
return p1
}
override fun onMarkerClick(p0: Marker?) = true
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
MY_PERMISSION_FINE_LOCATION -> if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//permission to access location grant
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
mMap.isMyLocationEnabled = true
}
}
//permission to access location denied
else {
Toast.makeText(
applicationContext,
"This app requires location permissions to be granted",
Toast.LENGTH_LONG
).show()
finish()
}
}
}
}