简体   繁体   English

在 Android Studio 上的 Kotlin 中使用 Mapbox 实现 Geojson 点以映射和定位我

[英]Implement Geojson points to map and locate me with Mapbox, in Kotlin on Android Studio

My question is about Mapbox.我的问题是关于 Mapbox。 In this period I am working on an ANDROID application based on mapbox, using Kotlin and Fragments and my problem concerns the visualization of points on the map itself.在此期间,我正在研究基于 mapbox 的 ANDROID 应用程序,使用 Kotlin 和 Fragments,我的问题涉及地图本身上点的可视化。 That is my need is to be able to show points on the map through a GEOJSON file, for now I have been able to see the map in full in the application, but I cannot find a way to show the points taken from a GeoJson file and locate myself in the map via a button.那就是我的需要是能够通过 GEOJSON 文件在地图上显示点,现在我已经能够在应用程序中完整地看到地图,但是我找不到一种方法来显示从 GeoJson 文件中获取的点并通过按钮在地图中定位自己。 I should implement both functions in the fragment, so my problem is precisely that of not being able to show the points of a geojson file and find a way to locate myself in the map itself.我应该在片段中实现这两个功能,所以我的问题恰恰是无法显示 geojson 文件的点并找到一种方法来在地图本身中定位自己。 I await help if there is someone able to help me with this problem, I also leave the code of the fragment class in kotlin.如果有人能够帮助我解决这个问题,我会等待帮助,我还将片段类的代码保留在 kotlin 中。 Thanks everyone in advance !!提前谢谢大家!!

FRAGMENT HOME碎片之家

class HomeFragment : Fragment() {

        private var mapView: MapView? = null

        @Nullable
        override fun onCreateView(
            inflater: LayoutInflater,
            @Nullable container: ViewGroup?,
            @Nullable savedInstanceState: Bundle?
        ): View? {
            Mapbox.getInstance(
                context!!.applicationContext,
                "MIO CODICE MAPBOX"
            )
            val view: View = inflater.inflate(R.layout.fragment_home, container, false)
            mapView = view.findViewById<View>(R.id.mapView) as MapView
            mapView!!.onCreate(savedInstanceState)
            return view
        }

        override fun onResume() {
            super.onResume()
            mapView!!.onResume()
        }

        override fun onPause() {
            super.onPause()
            mapView!!.onPause()
        }

        override fun onSaveInstanceState(outState: Bundle) {
            super.onSaveInstanceState(outState)
            mapView!!.onSaveInstanceState(outState)
        }

        override fun onLowMemory() {
            super.onLowMemory()
            mapView!!.onLowMemory()
        }

        override fun onDestroyView() {
            super.onDestroyView()
            mapView!!.onDestroy()
        }
    }

HOST ACTIVITY :主办活动:

class HostActivity : AppCompatActivity() {
    lateinit var googleSignInClient: GoogleSignInClient
    private lateinit var navController: NavController
    private val mAuth: FirebaseAuth = FirebaseAuth.getInstance()
    private val db: FirebaseFirestore = FirebaseFirestore.getInstance()
    private lateinit var drawerLayout: DrawerLayout
    private lateinit var navViewBinding: DrawerHeaderLayoutBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.AppTheme)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_host)
        val toolbar = customToolbar
        setSupportActionBar(toolbar)

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build()
        googleSignInClient = GoogleSignIn.getClient(this, gso)

        drawerLayout = drawer_layout
        navViewBinding = DrawerHeaderLayoutBinding.inflate(layoutInflater, navView, true)
        val navHost =
            supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
        navController = navHost.navController

        val navInflater = navController.navInflater

        val graph = navInflater.inflate(R.navigation.main_graph)

        navController.addOnDestinationChangedListener { _, destination, _ ->
            if (destination.id == R.id.onBoarding ||
                destination.id == R.id.authFragment ||
                destination.id == R.id.loginFragment ||
                destination.id == R.id.signUpFragment
            ) {
                toolbar.visibility = View.GONE
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            } else {
                toolbar.visibility = View.VISIBLE
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
            }
        }
        if (!Prefs.getInstance(this)!!.hasCompletedWalkthrough!!) {
            if (mAuth.currentUser == null) {
                graph.startDestination = R.id.authFragment
            } else {
                getUserData()
                graph.startDestination = R.id.homeFragment
            }
        } else {
            graph.startDestination = R.id.onBoarding

        }
        navController.graph = graph

        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        navView.setupWithNavController(navController)
        navView.setNavigationItemSelectedListener {
            it.isChecked
            drawerLayout.closeDrawers()
            when (it.itemId) {
                R.id.action_logout -> {
                    MyApplication.currentUser!!.active = false
                    FirestoreUtil.updateUser(MyApplication.currentUser!!) {
                        mAuth.signOut()
                    }
                    googleSignInClient.signOut()
                    MyApplication.currentUser = null
                    navController.navigate(R.id.action_logout)
                }
            }
            true
        }
    }

    private fun getUserData() {

        val ref = db.collection("users").document(mAuth.currentUser!!.uid)

        ref.get().addOnSuccessListener {
            val userInfo = it.toObject(UserModel::class.java)
            navViewBinding.user = userInfo
            MyApplication.currentUser = userInfo
            MyApplication.currentUser!!.active = true
            FirestoreUtil.updateUser(MyApplication.currentUser!!) {
            }
        }.addOnFailureListener {
            val intent = Intent(this, MyApplication::class.java)
            startActivity(intent)
            finish()
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(navController, drawerLayout)
    }

}

You'll need to use a GeoJsonSource .您需要使用GeoJsonSource https://github.com/mapbox/mapbox-android-demo/search?q=GeoJsonSource shows how the demo app uses the source. https://github.com/mapbox/mapbox-android-demo/search?q=GeoJsonSource显示演示应用程序如何使用源。

https://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/basics/KotlinSupportMapFragmentActivity.kt ( Its XML layout file ) https://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/basics/KotlinSupportMapFragmentActivity.kt它的XML布局文件

Putting icons on the map.在地图上放置图标。 https://docs.mapbox.com/android/maps/examples/marker-symbol-layer/ . https://docs.mapbox.com/android/maps/examples/marker-symbol-layer/ Do all of the icon setup inside of the fragment's onStyleLoaded() callback as seen at https://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/basics/KotlinSupportMapFragmentActivity.kt#L51-L55在片段的onStyleLoaded()回调中执行所有图标设置,如https://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo /examples/basics/KotlinSupportMapFragmentActivity.kt#L51-L55

https://github.com/mapbox/mapbox-android-demo/search?q=loadGeojson shows how the demo app loads from a GeoJson file. https://github.com/mapbox/mapbox-android-demo/search?q=loadGeojson显示演示应用程序如何从 GeoJson 文件加载。 You could use coroutines instead of building out the AsyncTask.您可以使用协程而不是构建 AsyncTask。

Although it's in Java, https://docs.mapbox.com/android/maps/examples/show-a-users-location-on-a-fragment/ shows how to combine the Maps SDK's LocationComponent with a fragment.尽管它使用 Java,但https://docs.mapbox.com/android/maps/examples/show-a-users-location-on-a-fragment/展示了如何将 Maps SDK 的LocationComponent与片段组合在一起。

Regarding moving the camera to the device's last known location when a button is clicked, see my answer at https://stackoverflow.com/a/64159178/6358488关于在单击按钮时将相机移动到设备的最后一个已知位置,请参阅我在https://stackoverflow.com/a/64159178/6358488 上的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM