从我的应用程序启动 Google 地图,以使用不同颜色的标记根据经度和纬度显示多个标记

问题描述 投票:0回答:1

我试图在我的应用程序中的 Google 地图应用程序中显示多个带有经度和纬度的标记,并且我还尝试根据我必须显示的标记类型更改标记的颜色。

private fun launchMaps(ticketLocationDetails:List<TicketLocationDetails>){
    try {
        val random = Random
        for (item in ticketLocationDetails) {
            val color = generateRandomColor(random)
            colorList[item.meter_serial_no] = color
        }
        val customMapUrl = constructCustomMapUrl(ticketLocationDetails)

        // Launch Google Maps app with the custom map URL
        val mapIntentUri = Uri.parse("geo:0,0?q=${customMapUrl}")
        val mapIntent = Intent(Intent.ACTION_VIEW, mapIntentUri)
        mapIntent.setPackage("com.google.android.apps.maps")
        mapIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(mapIntent)
    }catch (e:Exception){
        Toast.makeText(requireContext(),"unable to launch google maps application",Toast.LENGTH_SHORT).show()
        e.printStackTrace()
    }
}

private fun constructCustomMapUrl(ticketLocationDetails: List<TicketLocationDetails>): String {
    return if (ticketLocationDetails.isNotEmpty()) {
        val centerCoordinateItem = ticketLocationDetails[0]
        // Construct markers for each point
        val markers = ticketLocationDetails.mapIndexed { index, point ->
            "&markers=7Clabel:${index+1}%7C${point.latitude},${point.longitude}&label=${Uri.encode(point.meter_serial_no)}"
        }.joinToString("")

        // Construct the rest of the URL with parameters
        val center = "${centerCoordinateItem.latitude},${centerCoordinateItem.longitude}" // Example center coordinate
        val zoomLevel = 10
        "https://www.google.com/maps/@$center,$zoomLevel?${markers}"
    }else{
        ""
    }
}
java android kotlin google-maps mobile-application
1个回答
0
投票

如果想要显示带有不同颜色标记(从代码动态生成)的 Google 地图,您需要使用 Google Maps Static API

这将为您提供静态图像(有几种格式),并支持任何颜色和 3 种尺寸的标记:

注意 URL 开头为 地图。googleapis.com/maps/api/staticmap

代码中的 URL 格式(以 www.google.com/maps/@ 开头的 URL)将带您进入 Google 地图 消费者应用程序 (maps.google.com),该应用程序不是一个API 并且不支持 Maps Static API 支持的任何参数。

如果您确实想将用户发送到 Google 地图,使用不会破坏您的应用程序的受支持 API(不可预测且根本没有通知),请改用 Google Maps URL(同样,不支持标记) .

© www.soinside.com 2019 - 2024. All rights reserved.