mafortis
mafortis

Reputation: 7138

How to generate marker array in Kotlin

I have 2 separate location data and I need to make array of them (I guess I need to make array of them, maybe you have better idea!) in order to add marker for both locations in google map.

Code

Locations part are commented

override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap

    // Try to obtain the map from the SupportMapFragment.
    if (ContextCompat.checkSelfPermission(
            requireContext(),
            android.Manifest.permission.ACCESS_FINE_LOCATION
        ) ==
        PackageManager.PERMISSION_GRANTED &&
        ContextCompat.checkSelfPermission(
            requireContext(),
            android.Manifest.permission.ACCESS_COARSE_LOCATION
        ) ==
        PackageManager.PERMISSION_GRANTED) {
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
    } else {
        Toast.makeText(context, "Allow location access", Toast.LENGTH_LONG).show();
    }

    mFusedLocationClient = context?.let { LocationServices.getFusedLocationProviderClient(it) }!!

    mFusedLocationClient.lastLocation
        .addOnSuccessListener { location: Location? ->
            if (location != null) {
                // Location 1 (current location of user)
                val driverLatLng = LatLng(location.latitude, location.longitude)
                mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(driverLatLng, 15f))
                // Zoom in, animating the camera.
                mMap!!.animateCamera(CameraUpdateFactory.zoomIn())
            }
        }
    //new

    // Location 2
    val geoCoder = Geocoder(context)
    var address = geoCoder.getFromLocationName(customerAddressArgument, 1)!![0]

    val customerLatLng= LatLng(address.latitude, address.longitude)
    mMap!!.addMarker(MarkerOptions().position(customerLatLng).title("Customer Location"))
    val cameraPosition = CameraPosition.Builder()
        .target(customerLatLng) // Sets the center of the map to Mountain View
        .zoom(17f)            // Sets the zoom
        .bearing(90f)         // Sets the orientation of the camera to east
        .tilt(30f)            // Sets the tilt of the camera to 30 degrees
        .build()              // Creates a CameraPosition from the builder
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}

What I need is to make array of: val driverLatLng = LatLng(location.latitude, location.longitude) and val customerLatLng = LatLng(address.latitude, address.longitude)

Any idea?

Upvotes: 0

Views: 682

Answers (2)

AZIM MOHAMAD
AZIM MOHAMAD

Reputation: 72

Here is the solution for adding marker to map

first get both location point like location2 and location2

val markerOptions = MarkerOptions()
// First marker
markerOptions.position(location1).icon(BitmapDescriptorFactory.fromBitmap

(BitmapFactory.decodeResource(resources, R.mipmap.ic_user_location)))
mMap.addMarker(markerOptions)
 
 // second marker
 markerOptions.position(location2).icon(BitmapDescriptorFactory.fromBitmap

(BitmapFactory.decodeResource(resources, R.mipmap.ic_user_location)))
mMap.addMarker(markerOptions)
// Move camera
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))

// resize icon
private fun resizeIcon():Bitmap{
    val height = 120
    val width = 60
    val b: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_map_marker)
    val smallMarker = Bitmap.createScaledBitmap(b, width, height, false)
    return smallMarker

}

Upvotes: 1

Amit pandey
Amit pandey

Reputation: 1195

Create

var markerList:ArrayList<Marker> = ArrayList()

Create your marker

 val marker =  MarkerOptions().position(latlng).anchor(
            0.5f,
            0.5f
        ).title(your tittle).snippet(it.event_id).icon(your marker icon)

//Than add markers into marker list

markerList.add(marker)

use for loop to set the marker into google map

     markerList.forEach{
      // set marker to your google map with it
     }
      

Upvotes: 0

Related Questions