Почему не работает карта yandex.mapkit.mapview. При нажатии в приложении на кнопку "карта", приложение вылетает

Рейтинг: 0Ответов: 0Опубликовано: 19.01.2023

Class:

import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.PointF
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.example.medmark.R
import com.yandex.mapkit.Animation
import com.yandex.mapkit.MapKit
import com.yandex.mapkit.MapKitFactory
import com.yandex.mapkit.geometry.Point
import com.yandex.mapkit.layers.ObjectEvent
import com.yandex.mapkit.map.*
import com.yandex.mapkit.map.Map
import com.yandex.mapkit.mapview.MapView
import com.yandex.mapkit.search.*
import com.yandex.mapkit.user_location.UserLocationLayer
import com.yandex.mapkit.user_location.UserLocationObjectListener
import com.yandex.mapkit.user_location.UserLocationView
import com.yandex.runtime.Error
import com.yandex.runtime.image.ImageProvider
import com.yandex.runtime.network.NetworkError
import com.yandex.runtime.network.RemoteError

class Karta : AppCompatActivity(), UserLocationObjectListener, Session.SearchListener, CameraListener {

    lateinit var mapview: MapView
    lateinit var probkibut: Button
    lateinit var locationmapkit: UserLocationLayer
    lateinit var searchEdit: EditText
    lateinit var searchManager: SearchManager
    lateinit var searchSession: Session

    private fun sumbitQuery(query: String) {
        searchSession = searchManager.submit(
            query,
            VisibleRegionUtils.toPolygon(mapview.map.visibleRegion),
            SearchOptions(),
            this
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MapKitFactory.setApiKey("key")
        MapKitFactory.initialize(this)
        setContentView(R.layout.karta)
        mapview = findViewById(R.id.mapview)
        mapview.map.move(
            CameraPosition(Point(54.345241, 84.209828), 11.0f, 0.0f, 0.0f),
            Animation(Animation.Type.SMOOTH, 4f), null
        )
        var mapKit: MapKit = MapKitFactory.getInstance()
        requestLocationPermission()

        var probki = mapKit.createTrafficLayer(mapview.mapWindow)
        probki.isTrafficVisible = false
        probkibut = findViewById(R.id.probkibut)
        probkibut.setOnClickListener {
            if (probki.isTrafficVisible == false) {
                probki.isTrafficVisible = true
                probkibut.setBackgroundResource(R.drawable.simpleblue)
            } else {
                probki.isTrafficVisible = false
                probkibut.setBackgroundResource(R.drawable.blueooff)
            }
        }

        locationmapkit = mapKit.createUserLocationLayer(mapview.mapWindow)
        locationmapkit.isVisible = true
        locationmapkit.setObjectListener(this)
        SearchFactory.initialize(this)
        searchManager = SearchFactory.getInstance().createSearchManager(SearchManagerType.COMBINED)
        mapview.map.addCameraListener(this)
        //searchEdit.findViewById(R.id.search_edit)
        searchEdit.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                sumbitQuery(searchEdit.text.toString())
            }
            false
        }

    }

    private fun requestLocationPermission() {
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                ),
                0
            )
            return

        }
    }


    override fun onStop() {
        mapview.onStop()
        MapKitFactory.getInstance().onStop()
        super.onStop()

    }

    override fun onStart() {
        mapview.onStart()
        MapKitFactory.getInstance().onStart()
        super.onStart()
    }

    override fun onObjectAdded(userLocationView: UserLocationView) {
        locationmapkit.setAnchor(
            PointF((mapview.width() * 0.5).toFloat(), (mapview.height() * 0.5).toFloat()),
            PointF((mapview.width() * 0.5).toFloat(), (mapview.height() * 0.83).toFloat())
        )
        userLocationView.arrow.setIcon(ImageProvider.fromResource(this, R.drawable.user_arrow))
        val picIcon = userLocationView.pin.useCompositeIcon()
        picIcon.setIcon(
            "Icon",
            ImageProvider.fromResource(this, R.drawable.search_result),
            IconStyle().setAnchor(PointF(0f, 0f))
                .setRotationType(RotationType.ROTATE).setZIndex(0f).setScale(1f)
        )
        picIcon.setIcon(
            "pin", ImageProvider.fromResource(this, R.drawable.nothing),
            IconStyle().setAnchor(PointF(0.5f, 05f)).setRotationType(RotationType.NO_ROTATION)
                .setZIndex(1f).setScale(0.5f)
        )
        userLocationView.accuracyCircle.fillColor = Color.BLUE and -0x66000001
    }

    override fun onObjectRemoved(p0: UserLocationView) {
        TODO("Not yet implemented")
    }

    override fun onObjectUpdated(p0: UserLocationView, p1: ObjectEvent) {
        TODO("Not yet implemented")
    }

    override fun onSearchResponse(response: Response) {
        val mapObjects: MapObjectCollection = mapview.map.mapObjects
        mapObjects.clear()
        for (searchResult in response.collection.children){
            val resultLocation = searchResult.obj!!.geometry[0].point!!
            if (response != null){
                mapObjects.addPlacemark(
                    resultLocation,
                    ImageProvider.fromResource(this, R.drawable.search_result)
                )
            }
        }
    }

    override fun onSearchError(error: Error) {
        var errorMessage = "Неизвестная ошибка!"
        if (error is RemoteError){
            errorMessage = "Беспроводная ошибка!"
        }else if (error is NetworkError){
            errorMessage = "Проблема с интернетом!"
        }
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show()
    }

    override fun onCameraPositionChanged(
        map: Map,
        cameraPosition: CameraPosition,
        cameraUpdateReason: CameraUpdateReason,
        finished: Boolean
    ) {
        if (finished) {
            sumbitQuery(searchEdit.text.toString())
        }
    }
}

xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.yandex.mapkit.mapview.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <EditText
        android:layout_marginTop="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:padding="3dp"
        android:id="@+id/search_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:maxLines="1"
        android:hint="  Поиск"
        android:gravity="start"
        android:background="@drawable/poisk"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <Button
        android:id="@+id/probkibut"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/blueooff"
        tools:ignore="SpeakableTextPresentCheck" />

    <androidx.cardview.widget.CardView
        app:cardCornerRadius="180dp"
        android:layout_marginBottom="30dp"
        android:layout_marginEnd="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="50dp"
        android:layout_height="50dp">

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

вторая ошибка Logcat? строка 37 вторая ошибка Logcat? строка 37


Ошибки в Logcat

[Ошибки в Logcat строка !скрин главного меню приложения (кнопка карта)37]3

Ответы

Ответов пока нет.