显示附近的标记

时间:2019-10-29 15:04:57

标签: android google-maps kotlin google-maps-markers

我已经在地图上定义了一个地点的标记,我只想在输入的位置上显示附近的标记

    val nangka = LatLng(14.669100, 121.108140)
    val north_edsa = LatLng(14.596530, 121.059441)
    val mandaluyong = LatLng(14.590030, 121.034700)
    val payatas = LatLng(14.628710, 121.063900)
    val bay_area = LatLng(14.537208, 120.990720)
    val paranaque = LatLng(14.447820, 121.023040)
    val muntinlupa = LatLng(14.590030, 121.034700)
    val feliz = LatLng(14.628710, 121.063900)
    val manila = LatLng(14.577701, 120.983741)
    val makati = LatLng(14.551714, 121.027288)
    val las_pinas = LatLng(14.453614, 120.975966)
    val katipunan = LatLng(14.639077, 121.074214)
    val raymundo = LatLng(14.572139, 121.083775)
    val commonwealth = LatLng(14.663149, 121.068543)
    val alabang = LatLng(14.428302, 121.027184)
    val antipolo = LatLng(14.5884681, 121.1574247)
    val main = LatLng(14.5890657, 121.07890606)
    val fairview = LatLng(14.7363483, 121.06002163)
    val low_antipolo = LatLng(14.62467319, 121.12265825)
    val gateway = LatLng(14.6219187, 121.05293074)
    val angono = LatLng(14.53093443, 121.15457696)
    val tandang_sora = LatLng(14.67962335, 121.01988032)
    val valenzuela = LatLng(14.68605018, 120.97691678)

1 个答案:

答案 0 :(得分:1)

您可以使用Location::distanceBetween获取到A点(用户位置)和B点(其他位置)的距离

您应将所有标记添加到Google Map,并将实例保留在每个标记上,然后将用户位置与每个标记的距离进行比较,并更改可见性状态

    val result = FloatArray(1) // First element will contains the distance between the locations
    val currentUserLocation: Location = getCurrentUserLocation()
    aMarkerList.forEach {
        val currentLatLng = it.getPosition()
        // Calculates the distance between two points
        Location.distanceBetween(
            currentUserLocation.latitude,
            currentUserLocation.longitude,
            currentLatLng.latitude,
            currentLatLng.longitude,
            result
        )
        val distance = result[0] // You could use result[0] directly
        it.isVisible = distance <= YOUR_DISTANCE
    }

更新

一旦Google地图准备就绪,您可以在以下位置存储标记以进行操作:

val nangka = LatLng(14.669100, 121.108140)
val northEdsa = LatLng(14.596530, 121.059441)

Marker nangkaMarker = mMap.addMarker(MarkerOptions().position(nangka));
Marker northEdsaMarker = mMap.addMarker(MarkerOptions().position(northEdsa));

aMarkerList.add(nangkaMarker)
aMarkerList.add(northEdsaMarker)
// And son on...

我很确定,下一个链接会很有帮助https://developers.google.com/maps/documentation/android-sdk/marker#customize_a_marker